Python的reportlab专门将数据使用生成PDF中的图形和文档功能,

下载ReportLab

https://pypi.python.org/simple/reportlab/

http://www.reportlab.com/software/opensource/rl-toolkit/download/

生成一个简单的helloworld

from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics import renderPDF d = Drawing(100, 100)
s = String(50, 50, 'Hello, world!', textAnchor='middle')
d.add(s) renderPDF.drawToFile(d, 'hello.pdf', 'A simple PDF file')

三条折线

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))
drawing.add(PolyLine(zip(times, high), strokeColor=colors.red))
drawing.add(PolyLine(zip(times, low), strokeColor=colors.green)) drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots')

最终的sunspot

from urllib import urlopen
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF URL = 'http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt'
COMMENT_CHARS = '#:' drawing = Drawing(400, 200)
data = []
for line in urlopen(URL).readlines():
if not line.isspace() and not line[0] in COMMENT_CHARS:
data.append([float(n) for n in line.split()]) pred = [row[2] for row in data]
high = [row[3] for row in data]
low = [row[4] for row in data]
times = [row[0] + row[1]/12.0 for row in data] lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = [zip(times, pred), zip(times, high), zip(times, low)]
lp.lines[0].strokeColor = colors.blue
lp.lines[1].strokeColor = colors.red
lp.lines[2].strokeColor = colors.green drawing.add(lp) drawing.add(String(250, 150, 'Sunspots',
fontSize=14, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report2.pdf', 'Sunspots')

最新文章

  1. Nginx笔记
  2. ssh简化后之事务管理
  3. Java8的伪共享和缓存行填充--@Contended注释
  4. Can't connect to local MySQL server through socket '/tmp/mysql.sock'
  5. 2 配置Ionic开发环境以及创建新的项目
  6. [POJ1936]All in All
  7. sqlserver 2005列转行
  8. jQuery each,避免使用js for循环
  9. openstack 创建虚拟机
  10. std::remove
  11. javaScript特效
  12. sqlserver、mysql、oracle各自的默认端口号
  13. Ruby 一些经常使用的细节
  14. C# Winform中的窗体传值
  15. connect by prior 递归算法
  16. Beta版本冲刺计划安排
  17. 理解WebKit和Chromium: 网页渲染的基本过程
  18. VC显示网页验证码、模拟CSDN网页登录
  19. CSS中background属性详解
  20. graphql详解

热门文章

  1. SQL Server中,with as使用介绍
  2. 图像局部显著性—点特征(GLOH)
  3. Reducing the Dimensionality of Data with Neural Networks:神经网络用于降维
  4. 【C++】颜色的设置
  5. 极客学院免费VIP
  6. Monad (functional programming)
  7. Android 性能测试初探(四)
  8. 01-Linux命令基础-第01天(命令基础,软件安装与卸载、磁盘管理)
  9. ls命令:显示文件或目录
  10. Centos 7, Torque 单节点部署