2011-04-19 3 views
0

je besoin d'une écriture d'un calendrier pour exceller comme ceci: enter image description hereComment écrire un calendrier pour exceller?

lignes de calendrier sont 8 pas 7. S'il vous plaît aidez-moi? EDITED: Je mets mon code: fonction de base du calendrier de dessin sur Excel.

def _make_calendar(self, row, column): 
    cal = calendar.Calendar() 
    for month in range(1, 13): 
     self._prepare_calendar_month(column); 
     row, merge_to = self._create_calendar_month(cal, month, row+1, column) 
     self._create_merged_cell(6, 6, column, column+merge_to, month, self.style_text_center_xf) 
     column += merge_to + 1 

Dessin de cellules bordées sur Excel

def _prepare_calendar_month(self, column): 
     data = None 
     for r in range(7, 15): 
      for c in range(column, column+5): 
       self._create_cell(r, c, data, self.style_text_center_xf) 

calendrier Tirage au sort sur Excel:

def _create_calendar_month(self, cal, month, row, column): 
     year = 2011 
     last_day_of_month = self._last_day_of_month(datetime(year, month, 1)) 
     merge_to = 0 
     for k, v in cal.itermonthdays2(year, month): 
      if k: 
       self._create_cell(row, column, k, self.style_text_center_xf) 
      row += 1 
      if row == 15: 
       row = 7 
       if k != 0: 
        column += 1 
       if last_day_of_month != k and k != 0: 
        merge_to += 1 
     return row, merge_t 

Création de fonctions cellulaires:

def _create_cell(self, row, column, data, style=None): 
    if not style: 
     style = self.style_text_xf 
    self.sheet.write(row, column, data, style) 

def _create_merged_cell(self, row1, row2, column1, column2, data, style=None): 
    if not style: 
     style = self.ezxf('font:bold on;align:wrap off,vert centre,horiz left;') 
    self.sheet.write_merge(row1, row2, column1, column2, data, style) 

Donc mon problème est mes fonctions travaillent faux. Lorsque vous modifiez la variable year dans la fonction _create_calendar_month, le dessin est incorrect. Aidez-moi, s'il vous plaît?

+0

Êtes-vous sur Windows? – Fenikso

+0

Non. Je suis sur Linux, en utilisant OpenOffice et Python 2.x. Est-ce que c'est très important? – Zeck

+0

Vous devez spécifier comment dériver la ligne à laquelle appartient le 1er janvier, en fonction de l'année civile. Quelle partie du problème rencontrez-vous: (a) calcul de ce que (numéro de ligne, colonne) chaque numéro de jour doit être affiché dans (b) écriture de ce numéro de jour dans le calcul (ligne, colonne) dans un fichier Excel (c) tous les deux? –

Répondre

1

Ce code:

import calendar 
import pprint 

year = 2011 
days_in_week = 8 

c = calendar.Calendar() 

# First month with zeroes to create full week 
l = list(c.itermonthdays(year, 1)) 

# Slice by days_in_week 
l2 = [[l[a*days_in_week:a*days_in_week+days_in_week] for a in range(len(l)/days_in_week + 1)]] 
# Add zeroes if needed and slice rest 
l2[-1][-1] += [0] * (days_in_week - (len(l2[-1][-1]))) 
if l2[-1][-1].count(0) == days_in_week: 
    l2[-1] = l2[-1][:-1] 

for month in range(2, 13): 
    # Days in month 
    l = range(1, calendar.monthrange(year, month)[1]+1) 

    # Add needed zeroes to the beginning 
    zeroes_at_end = l2[-1][-1].count(0) 
    l = [0] * ((days_in_week - zeroes_at_end) % days_in_week) + l 

    # Slice by days_in_week 
    l2 += [[l[a*days_in_week:a*days_in_week+days_in_week] for a in range(len(l)/days_in_week + 1)]] 
    # Add zeroes if needed and slice rest 
    l2[-1][-1] += [0] * (days_in_week - (len(l2[-1][-1]))) 
    if l2[-1][-1].count(0) == days_in_week: 
     l2[-1] = l2[-1][:-1] 

pprint.pprint(l2) 

donne ce résultat:

[[[0, 0, 0, 0, 0, 1, 2, 3], 
    [4, 5, 6, 7, 8, 9, 10, 11], 
    [12, 13, 14, 15, 16, 17, 18, 19], 
    [20, 21, 22, 23, 24, 25, 26, 27], 
    [28, 29, 30, 31, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 1, 2, 3, 4], 
    [5, 6, 7, 8, 9, 10, 11, 12], 
    [13, 14, 15, 16, 17, 18, 19, 20], 
    [21, 22, 23, 24, 25, 26, 27, 28]], 
[[1, 2, 3, 4, 5, 6, 7, 8], 
    [9, 10, 11, 12, 13, 14, 15, 16], 
    [17, 18, 19, 20, 21, 22, 23, 24], 
    [25, 26, 27, 28, 29, 30, 31, 0]], 
[[0, 0, 0, 0, 0, 0, 0, 1], 
    [2, 3, 4, 5, 6, 7, 8, 9], 
    [10, 11, 12, 13, 14, 15, 16, 17], 
    [18, 19, 20, 21, 22, 23, 24, 25], 
    [26, 27, 28, 29, 30, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 1, 2, 3], 
    [4, 5, 6, 7, 8, 9, 10, 11], 
    [12, 13, 14, 15, 16, 17, 18, 19], 
    [20, 21, 22, 23, 24, 25, 26, 27], 
    [28, 29, 30, 31, 0, 0, 0, 0]], 
[[0, 0, 0, 0, 1, 2, 3, 4], 
    [5, 6, 7, 8, 9, 10, 11, 12], 
    [13, 14, 15, 16, 17, 18, 19, 20], 
    [21, 22, 23, 24, 25, 26, 27, 28], 
    [29, 30, 0, 0, 0, 0, 0, 0]], 
[[0, 0, 1, 2, 3, 4, 5, 6], 
    [7, 8, 9, 10, 11, 12, 13, 14], 
    [15, 16, 17, 18, 19, 20, 21, 22], 
    [23, 24, 25, 26, 27, 28, 29, 30], 
    [31, 0, 0, 0, 0, 0, 0, 0]], 
[[0, 1, 2, 3, 4, 5, 6, 7], 
    [8, 9, 10, 11, 12, 13, 14, 15], 
    [16, 17, 18, 19, 20, 21, 22, 23], 
    [24, 25, 26, 27, 28, 29, 30, 31]], 
[[1, 2, 3, 4, 5, 6, 7, 8], 
    [9, 10, 11, 12, 13, 14, 15, 16], 
    [17, 18, 19, 20, 21, 22, 23, 24], 
    [25, 26, 27, 28, 29, 30, 0, 0]], 
[[0, 0, 0, 0, 0, 0, 1, 2], 
    [3, 4, 5, 6, 7, 8, 9, 10], 
    [11, 12, 13, 14, 15, 16, 17, 18], 
    [19, 20, 21, 22, 23, 24, 25, 26], 
    [27, 28, 29, 30, 31, 0, 0, 0]], 
[[0, 0, 0, 0, 0, 1, 2, 3], 
    [4, 5, 6, 7, 8, 9, 10, 11], 
    [12, 13, 14, 15, 16, 17, 18, 19], 
    [20, 21, 22, 23, 24, 25, 26, 27], 
    [28, 29, 30, 0, 0, 0, 0, 0]], 
[[0, 0, 0, 1, 2, 3, 4, 5], 
    [6, 7, 8, 9, 10, 11, 12, 13], 
    [14, 15, 16, 17, 18, 19, 20, 21], 
    [22, 23, 24, 25, 26, 27, 28, 29], 
    [30, 31, 0, 0, 0, 0, 0, 0]]] 

Vous devriez être bien à partir de là.

+0

Cool. Tu sauves ma journée. Merci beaucoup. – Zeck

Questions connexes