来源:http://blog.csdn.net/happyteafriends/article/details/9418341

转载原因:直接复制代码碰到点问题,等下记录下

官方介绍:

Generating PDFs from Wall Street to Wikipedia

We build solutions to generate rich, attractive and fully bespoke PDF documents at incredible speeds. This can let you serve personalised documents in real time, produce high-quality output, and support all kinds of delivery from web downloads through to personalised digital print.

简要来说,就是 快速实时生成丰富的、定制的PDF文档。用于实时个性化定制,生成高质量的输出,支持各种交付,可以从网络下载到个人输出等。

安装:

  1. #  yum install freetype-devel.x86_64
  2. #  yum install python-devel.x86_64
  3. #  pip install reportlab

如果不安装python-devel否则报错:

  1. #  error: command 'gcc' failed with exit status 1 Command /usr/bin/python -c "import setuptools;__file__='/tmp/pip-build-root/reportlab/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-oneoII-record/install-record.txt --single-version-externally-managed failed with error code 1 in /tmp/pip-build-root/reportlab

几个例子:

坐标图:

  1. from reportlab.lib import colors
  2. from urllib import urlopen
  3. from reportlab.graphics.shapes import *
  4. from reportlab.graphics.charts.lineplots import LinePlot
  5. from reportlab.graphics import renderPDF
  6. URL="http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt"
  7. COMMENT_CHARS='#:'
  8. drawing = Drawing(400,200)
  9. data = []
  10. for  line in  urlopen(URL).readlines():
  11. if not line.isspace() and not line[0] in COMMENT_CHARS:
  12. data.append([float(n) for n in line.split()])
  13. pred = [row[2] for row in data]
  14. high = [row[3] for row in data]
  15. low = [row[4] for row in data]
  16. times = [row[0] + row[1]/12.0 for row in data]
  17. lp = LinePlot()
  18. lp.x = 50
  19. lp.y = 50
  20. lp.height = 125
  21. lp.width = 300
  22. lp.data = [zip(times,pred),zip(times,high),zip(times,low)]
  23. drawing.add(lp)
  24. renderPDF.drawToFile(drawing,'report.pdf','Sunspots')
  25. def main():
  26. return 0
  27. if __name__ == '__main__':
  28. main()

坐标柱状图:

  1. from reportlab.lib.colors import Color, blue, red
  2. from reportlab.graphics.charts.legends import Legend, TotalAnnotator
  3. from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
  4. from standard_colors import pdf_chart_colors, setItems
  5. from reportlab.lib.validators import Auto
  6. from reportlab.graphics.charts.barcharts import VerticalBarChart
  7. class FactSheetHoldingsVBar(_DrawingEditorMixin,Drawing):
  8. def __init__(self,width=400,height=200,*args,**kw):
  9. apply(Drawing.__init__,(self,width,height)+args,kw)
  10. self._add(self,VerticalBarChart(),name='bar',validate=None,desc=None)
  11. self.bar.data             = [[4.22], [4.12], [3.65], [3.56], [3.49], [3.44], [3.07], [2.84], [2.76], [1.09]]
  12. self.bar.categoryAxis.categoryNames = ['Financials','Energy','Health Care','Telecoms','Consumer','Consumer 2','Industrials','Materials','Other','Liquid Assets']
  13. self.bar.categoryAxis.labels.fillColor = None
  14. self.bar.width                      = 200
  15. self.bar.height                     = 150
  16. self.bar.x                          = 30
  17. self.bar.y                          = 15
  18. self.bar.barSpacing                 = 5
  19. self.bar.groupSpacing               = 5
  20. self.bar.valueAxis.labels.fontName  = 'Helvetica'
  21. self.bar.valueAxis.labels.fontSize  = 8
  22. self.bar.valueAxis.forceZero        = 1
  23. self.bar.valueAxis.rangeRound       = 'both'
  24. self.bar.valueAxis.valueMax         = None#10#
  25. self.bar.categoryAxis.visible       = 1
  26. self.bar.categoryAxis.visibleTicks  = 0
  27. self.bar.barLabels.fontSize         = 6
  28. self.bar.valueAxis.labels.fontSize  = 6
  29. self.bar.strokeWidth                = 0.1
  30. self.bar.bars.strokeWidth           = 0.5
  31. n                                   = len(self.bar.data)
  32. setItems(n,self.bar.bars,'fillColor',pdf_chart_colors)
  33. #add and set up legend
  34. self._add(self,Legend(),name='legend',validate=None,desc=None)
  35. _ = ['Vodafone Group', 'UBS', 'British Petroleum', 'Royal bk of Scotland', 'HSBC Holdings', 'Total Elf Fina', 'Repsol', 'Novartis', 'BNP Paribas', 'Schneider Electric' ]
  36. self.legend.colorNamePairs  = [(Auto(chart=self.bar),(t,'%.2f'% d[0])) for t,d in zip(_,self.bar.data)]
  37. self.legend.columnMaximum   = 10
  38. self.legend.fontName        = 'Helvetica'
  39. self.legend.fontSize        = 5.5
  40. self.legend.boxAnchor       = 'w'
  41. self.legend.x               = 260
  42. self.legend.y               = self.height/2
  43. self.legend.dx              = 8
  44. self.legend.dy              = 8
  45. self.legend.alignment       = 'right'
  46. self.legend.yGap            = 0
  47. self.legend.deltay          = 11
  48. self.legend.dividerLines    = 1|2|4
  49. self.legend.subCols.rpad    = 10
  50. self.legend.dxTextSpace     = 5
  51. self.legend.strokeWidth     = 0
  52. self.legend.dividerOffsY    = 6
  53. self.legend.colEndCallout   = TotalAnnotator(rText='%.2f'%sum([x[0] for x in self.bar.data]), fontName='Helvetica-Bold', fontSize=self.legend.fontSize*1.1)
  54. self.legend.colorNamePairs  = [(self.bar.bars[i].fillColor, (self.bar.categoryAxis.categoryNames[i][0:20], '%0.2f' % self.bar.data[i][0])) for i in range(len(self.bar.data))]
  55. def main():
  56. drawing = FactSheetHoldingsVBar()
  57. drawing.save(formats=['pdf'],outDir='.',fnRoot=None)
  58. drawing.save(formats=['png'],outDir='.',fnRoot=None)
  59. return 0
  60. if __name__ == '__main__':
  61. main()

绘制饼图:

  1. from reportlab.graphics.charts.piecharts import Pie
  2. from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
  3. from reportlab.lib.colors import Color, magenta, cyan
  4. class pietests(_DrawingEditorMixin,Drawing):
  5. def __init__(self,width=400,height=200,*args,**kw):
  6. Drawing.__init__(self,width,height,*args,**kw)
  7. self._add(self,Pie(),name='pie',validate=None,desc=None)
  8. self.pie.sideLabels       = 1
  9. self.pie.labels           = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5']
  10. self.pie.data             = [20, 10, 5, 5, 5]
  11. self.pie.width            = 140
  12. self.pie.height           = 140
  13. self.pie.y                = 35
  14. self.pie.x                = 125
  15. def main():
  16. drawing = pietests()
  17. # you can do all sorts of things to drawing, lets just save it as pdf and png.
  18. drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)
  19. return 0
  20. if __name__ == '__main__':
  21. main()

效果:

官网:

http://www.reportlab.com/

文档:

http://www.reportlab.com/software/documentation/

示例:

http://www.reportlab.com/snippets/

python绘图工具reportlab介绍相关推荐

  1. 海龟编程 python绘图工具turtle库的用法 turtle库使用方法大全,画笔设置 画布设置 绘图设置,画笔粗细,画笔颜色, 画笔速度。Python二级必须会的命令(已获取证书)

    目录 海龟编程 python绘图工具turtle库的用法 画布: 画笔 画笔运动命令: 画笔的控制命令: 全局控制命令: 简单turtle绘图示例: 圆中方: 三色同心圆: 四个圆中方: 螺旋正方: ...

  2. python海龟绘图代码大全-python海龟绘图的例子 python绘图工具用法

    python turtle模块即海龟绘图的使用方法,对于需要进行图形编程会有一定的借鉴价值. python turtle模块简介: python2.6版本中引入的一个简单的绘图工具,叫做海龟绘图(Tu ...

  3. python绘图工具基础-matplotlib学习之基本使用

    matplotlib学习之基本使用 1.figure学习2.设置坐标轴3.Legend 图例4.Annotation 标注5.tick能见度 1.figure学习 导包 import matplotl ...

  4. 01 熟悉python绘图工具——matplotlib

    一.简单案例 Matplotlib的图像是画在figure,每一个figure又包含了一个或多个axes(axes可用于指定绘图的子区域) import matplotlib.pyplot as pl ...

  5. Python开发工具PyCharm介绍

    PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...

  6. python使用工具简介介绍

    我从研究生开学以来就开始在学python,现在来简单分享下一些基本的使用命令和快捷方式 Pycharm: 运行程序 ctrl+alt+F10 删除一行ctrl+D 注释ctrl+/ 安装python所 ...

  7. python打包工具 cx_Freeze介绍

    原理 Python 脚本在装有 Python 的系统中可以直接双击运行,但绝大多数普通用户并没有配置此类环境,而编译为可执行二进制文件后,用户无需预先安装 Python 及依赖库即可像运行普通程序一样 ...

  8. Python-turtle标准库知识小结(python绘图工具)

    _________________________________________________________________________ turtle:海龟(海龟库) 使用之前需要导入库:i ...

  9. jsplumb php,jsplumb 中文教程 连线绘图工具库介绍 附简单在线demo与实战项目

    1. jsplumb 中文基础教程 后续的更新会直接在github上,如需查看最新文档,请直接访问原始仓库. 另外用了七牛的测试域名做图片托管,现在发现很多图片都无法显示了,建议直接参看git仓库里的 ...

  10. python中plotly_Python绘图工具Plotly的简单使用

    1.Plotly被称为史上最好的绘图工具之一,为了更好的展示金融数据的复杂性. Plotly的官方网站为:https://plot.ly/ python量化的关键是金融数据可视化,无论是传统的K线图, ...

最新文章

  1. python输入名字、输出欢迎你_python笔记3-输出输入、字符串格式化
  2. 2021-01-27 CentOS系统将UTC时间修改为CST时间方法
  3. Flutter入门:Image组件
  4. javaweb之Filter详解
  5. CSS行高line-height属性理解及应用
  6. android studio 汉化包 美化包
  7. java程序单词薄课程设计,Java程序设计课程设计
  8. MongoDB 清理数据
  9. KGB知识图谱在企业活动中能够实现那些运用
  10. 免费ftp服务器软件,实用的3款免费ftp服务器软件
  11. 新手小白如何购买阿里云服务器(2021新版详细图文教程)
  12. rnn 循环神经网络
  13. 郭德纲相声里的插入广告
  14. 如何优雅的使用C语言绘制一只小猪佩奇
  15. 内嵌html5,显示:内嵌HTML5元素
  16. Non-local的一些理解
  17. 新浪微博图床架构解析
  18. html5复合选择器,传智播客解读Css基本选择器与复合选择器
  19. Git Cheat Sheet——Git的常用命令和最佳做法
  20. 基于三维GIS技术的矢量地图动态LOD渲染方法研究现状

热门文章

  1. java常用的组件和框架总结
  2. idea关联本地的svn项目
  3. DHCPv6 snooping
  4. word批量转pdf,word批量转pdf步骤
  5. html css画圆形进度条,使用 css3 实现圆形进度条的方法
  6. android 串口调试助手源码,GitHub - Michelle0716/SerialPortHelper: Android 串口调试助手
  7. vscode unins000.exe报错,尝试在目标目录创造文件时发生错误
  8. FasterRCNN详解
  9. ssm 竞赛管理系统
  10. matlab汉明窗dft,Mitre_sfr代码注解(四) LSF / 汉明窗 / SFR(DFT)计算