用户工具

站点工具


python-files:pdf-calendar

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
python-files:pdf-calendar [2017/04/01 06:01]
admin [Reference]
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(0rows+1):
-    canvas.line(wmar,​ hmar, wmar+(boxwidth*lastweek),​ hmar) +
-    #now, for all complete rows, draw the bottom line +
-    ​for row in range(1len(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, startyx, 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, ystr(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)
python-files/pdf-calendar.1491026485.txt.gz · 最后更改: 2017/04/01 06:01 由 admin