Win7,64位,Python使用reportlab画图并生成为pdf文件

3/8/2017来源:ASP.NET技巧人气:1665

参考《Python基础教程(第二版)》

选用reportlab,因为它很容易使用并且为pdf中的图形和文档生成提供了丰富的功能。

reportlab的安装,参见:Win7,64位下Python3.5.2,安装reportlab(whl文件)

安装reportlab成功后,可以像下面这样导入reportlab模块:

>>> import reportlab >>> 

程序步骤:

·分析数据(数据以一系列元组的形式放入源代码);

·创建基于数据的pdf文件。

太阳黑子图形程序(sunspots_roto.py)

from reportlab.lib import colors
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF

data = [
#   year month PRedicted high low
    (2007, 8, 113.2, 114.2, 112.2),
    (2007, 9, 112.8, 115.8, 109.8),
    (2007, 10, 111.0, 116.0, 106.0),
    (2007, 11, 109.8, 116.8, 102.8),
    (2007, 12, 107.3, 115.3, 99.3),
    (2008, 1, 105.2, 114.2, 96.2),
    (2008, 2, 104.1, 114.1, 94.1),
    (2008, 3, 99.9, 110.9, 88.9),
    (2008, 4, 94.8, 106.8, 82.8),
    (2008, 5, 91.2, 104.2, 78.2),
    ]

drawing = Drawing(200, 150)

pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]
low = [row[4]-40 for row in data]
times = [200*((row[0] + row[1]/12.0) - 2007)-110 for row in data]

#drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue))   #Python2.x
#drawing.add(PolyLine(zip(times, high), strokeColor=colors.red))
#drawing.add(PolyLine(zip(times, low), strokeColor=colors.green))
drawing.add(PolyLine(list(zip(times, pred)), strokeColor=colors.blue))#Python3.x
drawing.add(PolyLine(list(zip(times, high)), strokeColor=colors.red))
drawing.add(PolyLine(list(zip(times, low)), strokeColor=colors.green))
drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red))

renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots')

结果显示如下: