| 后一修订版 | 前一修订版 | ||
|
python-files:pdf-calendar [2017/04/01 04:59] admin 创建 |
python-files:pdf-calendar [2017/04/01 06:08] (当前版本) admin |
||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Python 使用reportlab 创建PDF格式日历 ====== | ====== Python 使用reportlab 创建PDF格式日历 ====== | ||
| + | 使用到的模块有: | ||
| - | ===== Python 源代码 ===== | + | * calendar (python self contained) |
| + | * reportlab | ||
| + | ===== 功能修改 ===== | ||
| + | 做了下面的一些修改。 | ||
| + | |||
| + | ==== 字体 ==== | ||
| + | |||
| + | 在源代码里面增加了字体部分: | ||
| <code python> | <code python> | ||
| - | #!/usr/bin/env python | + | canvas.setFont("Times-Roman", 20) |
| - | """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 | + | <code python> |
| - | added, but it would be trivial to do so. Furthermore, this script is pretty | + | weekheader = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; |
| - | hacky, and could use some refactoring, but it works for what it's intended | + | </code> |
| - | to do. | + | |
| - | Created by Bill Mill on 11/16/05, this script is in the public domain. There | + | 日历表格中行数增加一行,表格的线也都补齐了。 |
| - | are no express warranties, so if you mess stuff up with this script, it's not | + | |
| - | my fault. | + | 生成的日历PDF文件为: {{ :python-files:python-generate-printable-calendar.pdf |}} |
| + | |||
| + | ===== Python 源代码 ===== | ||
| + | |||
| + | |||
| + | |||
| + | <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 | ||
| 行 49: | 行 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] | ||
| 行 56: | 行 65: | ||
| raise NoCanvasError | raise NoCanvasError | ||
| monthname = time.strftime("%B", time.strptime(str(month), "%m")) | monthname = time.strftime("%B", time.strptime(str(month), "%m")) | ||
| + | calendar.setfirstweekday(calendar.SUNDAY) | ||
| cal = calendar.monthcalendar(year, month) | cal = calendar.monthcalendar(year, month) | ||
| 行 66: | 行 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]) | ||
| 行 80: | 行 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: | ||
| 行 124: | 行 127: | ||
| if __name__ == "__main__": | if __name__ == "__main__": | ||
| #create a December, 2005 PDF | #create a December, 2005 PDF | ||
| - | c = createCalendar(12, 2005, 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(1, 2006, canvas=c) | + | createCalendar(5, 2017, canvas=c) |
| c.save() | c.save() | ||
| </code> | </code> | ||
| + | ===== Reference ===== | ||
| + | * 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 | ||