开发者

How to write a calendar to excel?

开发者 https://www.devze.com 2023-02-27 02:48 出处:网络
I need a write a calendar to excel like this: Calendar rows are 8 not 7. Please help me? EDITED: I\'m put my code: Basic function of drawing calendar on excel.

I need a write a calendar to excel like this:

How to write a calendar to excel?

Calendar rows are 8 not 7. Please help me? EDITED: I'm put my code: Basic function of drawing calendar on 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

Drawing bordered cells on 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)

Draw calendar on 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, col开发者_运维技巧umn, 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

Creating cell functions:

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)

So my problem is my functions are working wrong. When you change year variable in _create_calendar_month function it drawing wrong. Please help me?


This 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)

Gives this result:

[[[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]]]

You should be fine from there.


Option Explicit
    Dim ThisDay As Date
    Dim ThisYear, ThisMth As Date
    Dim CreateCal As Boolean
    Dim i As Integer

Private Sub HelpLabel_Click()

End Sub

Private Sub UserForm_Initialize()
    Application.EnableEvents = False
    'starts the form on todays date
    ThisDay = Date
    ThisMth = Format(ThisDay, "mm")
    ThisYear = Format(ThisDay, "yyyy")
    For i = 1 To 12
        CB_Mth.AddItem Format(DateSerial(Year(Date),     Month(Date) + i, 0), "mmmm")
    Next
    CB_Mth.ListIndex = Format(Date, "mm") - Format(Date,     "mm")
    For i = -20 To 50
        If i = 1 Then CB_Yr.AddItem Format((ThisDay),     "yyyy") Else CB_Yr.AddItem _
            Format((DateAdd("yyyy", (i - 1), ThisDay)), "yyyy")
    Next
    CB_Yr.ListIndex = 21
    'Builds the calendar with todays date
    CalendarFrm.Width = CalendarFrm.Width
    CreateCal = True
    Call Build_Calendar
    Application.EnableEvents = True
End Sub
Private Sub CB_Mth_Change()
    'rebuilds the calendar when the month is changed by     the user
    Build_Calendar
End Sub
Private Sub CB_Yr_Change()
    'rebuilds the calendar when the year is changed by the     user
    Build_Calendar
End Sub
Private Sub Build_Calendar()
    'the routine that actually builds the calendar each     time
    If CreateCal = True Then
    CalendarFrm.Caption = " " & CB_Mth.Value & " " &     CB_Yr.Value
    'sets the focus for the todays date button
    CommandButton1.SetFocus
    For i = 1 To 42
        If i < Weekday((CB_Mth.Value) & "/1/" &     (CB_Yr.Value)) Then
            Controls("D" & (i)).Caption =     Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" &     (CB_Yr.Value))), _
                ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))),     "d")
            Controls("D" & (i)).ControlTipText =     Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" &     (CB_Yr.Value))), _
                ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))),     "m/d/yy")
        ElseIf i >= Weekday((CB_Mth.Value) & "/1/" &     (CB_Yr.Value)) Then
            Controls("D" & (i)).Caption =     Format(DateAdd("d", (i - Weekday((CB_Mth.Value) _
                & "/1/" & (CB_Yr.Value))), ((CB_Mth.Value)     & "/1/" & (CB_Yr.Value))), "d")
            Controls("D" & (i)).ControlTipText =     Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" &     (CB_Yr.Value))), _
                ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))),     "m/d/yy")
        End If
        If Format(DateAdd("d", (i - Weekday((CB_Mth.Value)     & "/1/" & (CB_Yr.Value))), _
        ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "mmmm")     = ((CB_Mth.Value)) Then
            If Controls("D" & (i)).BackColor <> &H80000016     Then Controls("D" & (i)).BackColor = &H80000018  '&H80000010
            Controls("D" & (i)).Font.Bold = True
        If Format(DateAdd("d", (i - Weekday((CB_Mth.Value)     & "/1/" & (CB_Yr.Value))), _
            ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))),     "m/d/yy") = Format(ThisDay, "m/d/yy") Then Controls("D" &     (i)).SetFocus
        Else
            If Controls("D" & (i)).BackColor <> &H80000016     Then Controls("D" & (i)).BackColor = &H8000000F
            Controls("D" & (i)).Font.Bold = False
        End If
    Next
    End If
End Sub
Private Sub D1_Click()
    'this sub and the ones following represent the buttons     for days on the form
    'retrieves the current value of the individual     controltiptext and
    'places it in the active cell
    ActiveCell.Value = D1.ControlTipText
    Unload Me
    'after unload you can call a different userform to     continue data entry
    'uncomment this line and add a userform named     UserForm2
    'Userform2.Show

End Sub
Private Sub D2_Click()
    ActiveCell.Value = D2.ControlTipText
    Unload Me

End Sub
Private Sub D3_Click()
    ActiveCell.Value = D3.ControlTipText
    Unload Me

End Sub
Private Sub D4_Click()
    ActiveCell.Value = D4.ControlTipText
    Unload Me

End Sub
Private Sub D5_Click()
    ActiveCell.Value = D5.ControlTipText
    Unload Me

End Sub
Private Sub D6_Click()
    ActiveCell.Value = D6.ControlTipText
    Unload Me

End Sub
Private Sub D7_Click()
    ActiveCell.Value = D7.ControlTipText
    Unload Me

End Sub
Private Sub D8_Click()
    ActiveCell.Value = D8.ControlTipText
    Unload Me

End Sub
Private Sub D9_Click()
    ActiveCell.Value = D9.ControlTipText
    Unload Me

End Sub
Private Sub D10_Click()
    ActiveCell.Value = D10.ControlTipText
    Unload Me

End Sub
Private Sub D11_Click()
    ActiveCell.Value = D11.ControlTipText
    Unload Me

End Sub
Private Sub D12_Click()
    ActiveCell.Value = D12.ControlTipText
    Unload Me

End Sub
Private Sub D13_Click()
    ActiveCell.Value = D13.ControlTipText
    Unload Me

End Sub
Private Sub D14_Click()
    ActiveCell.Value = D14.ControlTipText
    Unload Me

End Sub
Private Sub D15_Click()
    ActiveCell.Value = D15.ControlTipText
    Unload Me

End Sub
Private Sub D16_Click()
    ActiveCell.Value = D16.ControlTipText
    Unload Me

End Sub
Private Sub D17_Click()
    ActiveCell.Value = D17.ControlTipText
    Unload Me

End Sub
Private Sub D18_Click()
    ActiveCell.Value = D18.ControlTipText
    Unload Me

End Sub
Private Sub D19_Click()
    ActiveCell.Value = D19.ControlTipText
    Unload Me

End Sub
Private Sub D20_Click()
    ActiveCell.Value = D20.ControlTipText
    Unload Me

End Sub
Private Sub D21_Click()
    ActiveCell.Value = D21.ControlTipText
    Unload Me

End Sub
Private Sub D22_Click()
    ActiveCell.Value = D22.ControlTipText
    Unload Me

End Sub
Private Sub D23_Click()
    ActiveCell.Value = D23.ControlTipText
    Unload Me

End Sub
Private Sub D24_Click()
    ActiveCell.Value = D24.ControlTipText
    Unload Me

End Sub
Private Sub D25_Click()
    ActiveCell.Value = D25.ControlTipText
    Unload Me

End Sub
Private Sub D26_Click()
    ActiveCell.Value = D26.ControlTipText
    Unload Me

End Sub
Private Sub D27_Click()
    ActiveCell.Value = D27.ControlTipText
    Unload Me

End Sub
Private Sub D28_Click()
    ActiveCell.Value = D28.ControlTipText
    Unload Me

End Sub
Private Sub D29_Click()
    ActiveCell.Value = D29.ControlTipText
    Unload Me

End Sub
Private Sub D30_Click()
    ActiveCell.Value = D30.ControlTipText
    Unload Me

End Sub
Private Sub D31_Click()
    ActiveCell.Value = D31.ControlTipText
    Unload Me

End Sub
Private Sub D32_Click()
    ActiveCell.Value = D32.ControlTipText
    Unload Me

End Sub
Private Sub D33_Click()
    ActiveCell.Value = D33.ControlTipText
    Unload Me

End Sub
Private Sub D34_Click()
    ActiveCell.Value = D34.ControlTipText
    Unload Me

End Sub
Private Sub D35_Click()
    ActiveCell.Value = D35.ControlTipText
    Unload Me

End Sub
Private Sub D36_Click()
    ActiveCell.Value = D36.ControlTipText
    Unload Me

End Sub
Private Sub D37_Click()
    ActiveCell.Value = D37.ControlTipText
    Unload Me

End Sub
Private Sub D38_Click()
    ActiveCell.Value = D38.ControlTipText
    Unload Me

End Sub
Private Sub D39_Click()
    ActiveCell.Value = D39.ControlTipText
    Unload Me

End Sub
Private Sub D40_Click()
    ActiveCell.Value = D40.ControlTipText
    Unload Me

End Sub
Private Sub D41_Click()
    ActiveCell.Value = D41.ControlTipText
    Unload Me

End Sub
Private Sub D42_Click()
    ActiveCell.Value = D42.ControlTipText
    Unload Me

End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消