这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
python-files:pdf-calendar [2017/04/01 05:22] admin [Python 源代码] |
python-files:pdf-calendar [2017/04/01 06:08] (当前版本) admin |
||
---|---|---|---|
行 5: | 行 5: | ||
* calendar (python self contained) | * calendar (python self contained) | ||
* reportlab | * reportlab | ||
+ | |||
+ | ===== 功能修改 ===== | ||
+ | 做了下面的一些修改。 | ||
+ | |||
+ | ==== 字体 ==== | ||
在源代码里面增加了字体部分: | 在源代码里面增加了字体部分: | ||
- | <code> | + | <code python> |
- | canvas.setFont("Times-Roman", 20) | + | canvas.setFont("Times-Roman", 20) |
</code> | </code> | ||
- | ===== Python 源代码 ===== | + | ==== 增加日历的星期名称 ==== |
<code python> | <code python> | ||
- | #!/usr/bin/env python | + | weekheader = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; |
- | """Create a PDF calendar. | + | </code> |
- | This script requires Python and Reportlab | + | 日历表格中行数增加一行,表格的线也都补齐了。 |
- | ( http://reportlab.org/rl_toolkit.html ). Tested only with Python 2.4 and | + | |
- | Reportlab 1.2. | + | |
- | See bottom of file for an example of usage. No command-line interface has been | + | 生成的日历PDF文件为: {{ :python-files:python-generate-printable-calendar.pdf |}} |
- | added, but it would be trivial to do so. Furthermore, this script is pretty | + | |
- | hacky, and could use some refactoring, but it works for what it's intended | + | |
- | to do. | + | |
- | Created by Bill Mill on 11/16/05, this script is in the public domain. There | + | ===== Python 源代码 ===== |
- | are no express warranties, so if you mess stuff up with this script, it's not | + | |
- | my fault. | + | |
+ | |||
+ | <code python> | ||
- | If you have questions or comments or bugfixes or flames, please drop me a line | ||
- | at bill.mill@gmail.com . | ||
- | """ | ||
from reportlab.lib import pagesizes | from reportlab.lib import pagesizes | ||
from reportlab.pdfgen.canvas import Canvas | from reportlab.pdfgen.canvas import Canvas | ||
行 58: | 行 57: | ||
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: | 行 76: | ||
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: | 行 89: | ||
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: | 行 127: | ||
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: | 行 135: | ||
===== 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 | ||