这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 后一修订版 两侧同时换到之后的修订记录 | ||
python-files:pdf-calendar [2017/04/01 05:22] admin [Python 源代码] |
python-files:pdf-calendar [2017/04/01 06:03] admin [Python 源代码] |
||
---|---|---|---|
行 58: | 行 58: | ||
size: size, in points of the canvas to write on | size: size, in points of the canvas to write on | ||
""" | """ | ||
+ | weekheader = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; | ||
if type(month) == type(''): | if type(month) == type(''): | ||
month = time.strptime(month, "%b")[1] | month = time.strptime(month, "%b")[1] | ||
行 76: | 行 77: | ||
height = height - 50 | height = height - 50 | ||
- | canvas.setFont("Times-Roman", 20) | ||
#margins | #margins | ||
- | wmar, hmar = width/50, height/50 | + | wmar, hmar = width/45, height/45 |
#set up constants | #set up constants | ||
width, height = width - (2*wmar), height - (2*hmar) | width, height = width - (2*wmar), height - (2*hmar) | ||
- | rows, cols = len(cal), 7 | + | rows, cols = len(cal) + 1, 7 ;# with header |
lastweek = nonzero(cal[-1]) | lastweek = nonzero(cal[-1]) | ||
firstweek = nonzero(cal[0]) | firstweek = nonzero(cal[0]) | ||
行 90: | 行 90: | ||
boxwidth = floor(width/7) | boxwidth = floor(width/7) | ||
- | #draw the bottom line | + | for row in range(0, rows+1): |
- | canvas.line(wmar, hmar, wmar+(boxwidth*lastweek), hmar) | + | |
- | #now, for all complete rows, draw the bottom line | + | |
- | for row in range(1, len(cal[1:-1]) + 1): | + | |
y = hmar + (row * rowheight) | y = hmar + (row * rowheight) | ||
canvas.line(wmar, y, wmar + (boxwidth * 7), y) | canvas.line(wmar, y, wmar + (boxwidth * 7), y) | ||
- | #now draw the top line of the first full row | ||
- | y = hmar + ((rows-1) * rowheight) | ||
- | canvas.line(wmar, y, wmar + (boxwidth * 7), y) | ||
- | #and, then the top line of the first row | ||
- | startx = wmar + (boxwidth * (7-firstweek)) | ||
- | endx = startx + (boxwidth * firstweek) | ||
- | y = y + rowheight | ||
- | canvas.line(startx, y, endx, y) | ||
- | #now draw the vert lines | ||
for col in range(8): | for col in range(8): | ||
#1 = don't draw line to first or last; 0 = do draw | #1 = don't draw line to first or last; 0 = do draw | ||
last, first = 1, 1 | last, first = 1, 1 | ||
- | if col <= lastweek: last = 0 | ||
- | if col >= 7 - firstweek: first = 0 | ||
x = wmar + (col * boxwidth) | x = wmar + (col * boxwidth) | ||
- | starty = hmar + (last * rowheight) | + | endy = hmar + (rows * rowheight) |
- | endy = hmar + (rows * rowheight) - (first * rowheight) | + | canvas.line(x, hmar, x, endy) |
- | canvas.line(x, starty, x, endy) | + | |
+ | #now fill in the week day abbr | ||
+ | canvas.setFont("Helvetica-Bold", 25) | ||
+ | x = wmar + floor(boxwidth/2) | ||
+ | y = hmar + (rows * rowheight) - floor(rowheight/2) | ||
+ | for abbr in weekheader: | ||
+ | canvas.drawCentredString(x, y, str(abbr)) | ||
+ | x += boxwidth | ||
#now fill in the day numbers and any data | #now fill in the day numbers and any data | ||
+ | canvas.setFont("Times-Roman", 20) | ||
x = wmar + 6 | x = wmar + 6 | ||
- | y = hmar + (rows * rowheight) - 25 | + | y = hmar + ((rows -1) * rowheight) - 25 ;# row - 1, represent week header |
for week in cal: | for week in cal: | ||
for day in week: | for day in week: | ||
行 134: | 行 128: | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
#create a December, 2005 PDF | #create a December, 2005 PDF | ||
- | c = createCalendar(4, 2017, filename="blog_calendar.pdf") | + | c = createCalendar(4, 2017, filename="python-generate-printable-calendar.pdf") |
#now add January, 2006 to the end | #now add January, 2006 to the end | ||
createCalendar(5, 2017, canvas=c) | createCalendar(5, 2017, canvas=c) | ||
行 142: | 行 136: | ||
===== Reference ===== | ===== Reference ===== | ||
- | * https://billmill.org/static/files/pdf_calendar.py | + | * https://billmill.org/static/files/pdf_calendar.py [[python-files:pdf-calendar-orignal-ref-back]] |
* http://stackoverflow.com/questions/7276017/producing-a-printable-calendar-with-python | * http://stackoverflow.com/questions/7276017/producing-a-printable-calendar-with-python | ||