python绘图工具reportlab介绍

python生成图文并茂的pdf--财务报表(一)--reportlab库简介解决安装出错问题 python生成图文并茂的pdf--以金融机构FOF财务报表为例。 阅读详情

来源: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.   
  4. #  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.   
  7. URL="http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt"  
  8. COMMENT_CHARS='#:'  
  9.   
  10. drawing = Drawing(400,200)  
  11. data = []  
  12. for  line in  urlopen(URL).readlines():  
  13.     if not line.isspace() and not line[0] in COMMENT_CHARS:  
  14.         data.append([float(n) for n in line.split()])  
  15.   
  16. pred = [row[2] for row in data]  
  17. high = [row[3] for row in data]  
  18. low = [row[4] for row in data]  
  19. times = [row[0] + row[1]/12.0 for row in data]  
  20.   
  21. lp = LinePlot()  
  22. lp.x = 50  
  23. lp.y = 50  
  24. lp.height = 125  
  25. lp.width = 300  
  26. lp.data = [zip(times,pred),zip(times,high),zip(times,low)]  
  27. drawing.add(lp)  
  28.   
  29. renderPDF.drawToFile(drawing,'report.pdf','Sunspots')  
  30.   
  31. def main():  
  32.   
  33.     return 0  
  34.   
  35. if __name__ == '__main__':  
  36.     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.   
  8. class FactSheetHoldingsVBar(_DrawingEditorMixin,Drawing):  
  9.     def __init__(self,width=400,height=200,*args,**kw):  
  10.         apply(Drawing.__init__,(self,width,height)+args,kw)  
  11.         self._add(self,VerticalBarChart(),name='bar',validate=None,desc=None)  
  12.         self.bar.data             = [[4.22], [4.12], [3.65], [3.56], [3.49], [3.44], [3.07], [2.84], [2.76], [1.09]]  
  13.         self.bar.categoryAxis.categoryNames = ['Financials','Energy','Health Care','Telecoms','Consumer','Consumer 2','Industrials','Materials','Other','Liquid Assets']  
  14.         self.bar.categoryAxis.labels.fillColor = None  
  15.         self.bar.width                      = 200  
  16.         self.bar.height                     = 150  
  17.         self.bar.x                          = 30  
  18.         self.bar.y                          = 15  
  19.         self.bar.barSpacing                 = 5  
  20.         self.bar.groupSpacing               = 5  
  21.         self.bar.valueAxis.labels.fontName  = 'Helvetica'  
  22.         self.bar.valueAxis.labels.fontSize  = 8  
  23.         self.bar.valueAxis.forceZero        = 1  
  24.         self.bar.valueAxis.rangeRound       = 'both'  
  25.         self.bar.valueAxis.valueMax         = None#10#  
  26.         self.bar.categoryAxis.visible       = 1  
  27.         self.bar.categoryAxis.visibleTicks  = 0  
  28.         self.bar.barLabels.fontSize         = 6  
  29.         self.bar.valueAxis.labels.fontSize  = 6  
  30.         self.bar.strokeWidth                = 0.1  
  31.         self.bar.bars.strokeWidth           = 0.5  
  32.         n                                   = len(self.bar.data)  
  33.         setItems(n,self.bar.bars,'fillColor',pdf_chart_colors)  
  34.         #add and set up legend  
  35.         self._add(self,Legend(),name='legend',validate=None,desc=None)  
  36.         _ = ['Vodafone Group', 'UBS', 'British Petroleum', 'Royal bk of Scotland', 'HSBC Holdings', 'Total Elf Fina', 'Repsol', 'Novartis', 'BNP Paribas', 'Schneider Electric' ]  
  37.         self.legend.colorNamePairs  = [(Auto(chart=self.bar),(t,'%.2f'% d[0])) for t,d in zip(_,self.bar.data)]  
  38.         self.legend.columnMaximum   = 10  
  39.         self.legend.fontName        = 'Helvetica'  
  40.         self.legend.fontSize        = 5.5  
  41.         self.legend.boxAnchor       = 'w'  
  42.         self.legend.x               = 260  
  43.         self.legend.y               = self.height/2  
  44.         self.legend.dx              = 8  
  45.         self.legend.dy              = 8  
  46.         self.legend.alignment       = 'right'  
  47.         self.legend.yGap            = 0  
  48.         self.legend.deltay          = 11  
  49.         self.legend.dividerLines    = 1|2|4  
  50.         self.legend.subCols.rpad    = 10  
  51.         self.legend.dxTextSpace     = 5  
  52.         self.legend.strokeWidth     = 0  
  53.         self.legend.dividerOffsY    = 6  
  54.         self.legend.colEndCallout   = TotalAnnotator(rText='%.2f'%sum([x[0] for x in self.bar.data]), fontName='Helvetica-Bold', fontSize=self.legend.fontSize*1.1)  
  55.         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))]  
  56.   
  57.   
  58.   
  59. def main():   
  60.     drawing = FactSheetHoldingsVBar()  
  61.     drawing.save(formats=['pdf'],outDir='.',fnRoot=None)  
  62.     drawing.save(formats=['png'],outDir='.',fnRoot=None)  
  63.     return 0  
  64.   
  65. if __name__ == '__main__':  
  66.     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.   
  5. class pietests(_DrawingEditorMixin,Drawing):  
  6.     def __init__(self,width=400,height=200,*args,**kw):  
  7.         Drawing.__init__(self,width,height,*args,**kw)  
  8.         self._add(self,Pie(),name='pie',validate=None,desc=None)  
  9.         self.pie.sideLabels       = 1  
  10.         self.pie.labels           = ['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5']  
  11.         self.pie.data             = [20, 10, 5, 5, 5]  
  12.         self.pie.width            = 140  
  13.         self.pie.height           = 140  
  14.         self.pie.y                = 35  
  15.         self.pie.x                = 125  
  16.   
  17.   
  18. def main():  
  19.     drawing = pietests()  
  20.     # you can do all sorts of things to drawing, lets just save it as pdf and png.  
  21.     drawing.save(formats=['pdf','png'],outDir='.',fnRoot=None)  
  22.     return 0  
  23.   
  24. if __name__ == '__main__':  
  25.     main()  

效果:


官网:

http://www.reportlab.com/

文档:

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

示例:

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

python 使用reportlab打造29页图文并茂pdf(全网reportlab最强pdf自动化生成代码) 这次项目所使用的代码如果同志们可以灵活使用,基本上可以解决百分之九十以上的pdf模板自动化生成。最近博主,做了一个项目,使用reportlab制作pdf,博主打算把代码分享出来,先看一下博主生成的pdf:以上就是,这次项目生成的pdf,使用的数据,我就不分享了,下面是代码。 阅读详情

相关推荐

python绘图工具reportlab介绍----续

上一篇转载<python绘图工具reportlab介绍>,直接运行代码有些问题,慢慢解决了,记录下 1,坐标图 在文章:http://www.aichengxu.com/view/49675 找到类似的,代码如下: from reportlab.lib import colors from reportlab.graphics.shapes import * from reportlab.g

heybob的专栏 4285

Python生成图文并茂的PDF报告

reportlab是Python的一个标准库,可以画图、画表格、编辑文字,最后可以输出PDF格式。它的逻辑和编辑一个word文档或者PPT很像。有两种方法:1)建立一个空白文档,然后在上面写文字、画图等;2)建立一个空白list,以填充表格的形式插入各种文本框、图片等,最后生成PDF文档。因为需要产生一份给用户看的报告,里面需要插入图片、表格等,所以采用的是第二种方法。安...

Python小二 2716

reportlab中文手册.pdf

reportlab中文手册.pdf

【Python】办公自动化:使用reportlab制作pdf报告

本博客重点内容:reportlab生成流文件格式、reportlab分页和图片流文件写入reportlab等。我讲一下我这个需求的来源,做的项目是一个地理空间查询和使用的系统,通过在前端调用高德地图api创建了一个查询区域,获取区域内的地理数据(数据库)。

黎明总是如期而至 3487

详尽的reportLab开发手册.pdf

使用reportLab生成pdf,详尽的用户手册,详细的开发指南,具有详细的开发说明,并且附有一个简单的例子

Microsoft Visual C++ 14.0 is required 的亲测最全解决方案(包括安装包丢失或损坏解决方法)

在用python调用别的语言(我用的是cache数据库)的方法时,在执行"python setup3.py install"方法时出现“error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”错误,以下是网上能找到的安装过程中“所有”解决方案: 1、首先需要下载Micr...

CDL2017CDL的博客 2万+

mac eclipse+pydev的reportlab工具包导入

今天需要学习report lab包的使用,这是在python基础教程里的项目2:画幅好画的一个工具包。我的系统是mac系统,使用的eclipse+pydev。 第一步  安装的文件: 安装成功: 第二步 已经将reportlab安装到了python 目录下,但是需要在eclipse中进行配置 成功的提示:不报错误

冬天里的雨 357

Windows10内核逆向-2-KiSystemCall64的调用流程剖析

本文深入剖析了Windows 10 x64系统中KiSystemCall64函数的完整调用流程。从用户态触发syscall指令开始,详细讲解了如何通过swapgs切换内核栈、构建KTRAP_FRAME陷阱帧、进行SMAP/CFG等安全检查,到最终通过SSDT寻址并执行目标系统服务的全过程,揭示了系统调用从Ring 3到Ring 0切换的核心机制与安全设计。

weixin_29209805的博客 93

Python使用ReportLab画图

本文主要介绍如何借助ReportLab画图。 首先看一下经典的hello word: #!/usr/bin/env python from reportlab.graphics.shapes import Drawing, String from reportlab.graphics import renderPDF d = Drawing(100, 100) s = String(50,

王伴农 6455

告别重启烦恼:用WSL2让Docker和VMware在Win10上真正共存

本文详细介绍了如何在Windows 10系统上,借助WSL2实现Docker与VMware的真正共存,彻底告别以往需要反复重启切换的烦恼。通过启用Hyper-V兼容模式并进行正确配置,开发者无需重启即可同时高效运行容器和虚拟机,显著提升开发与测试工作流的连续性和效率。

salt9的博客 998

【目标检测】IoU(交并比)详解及代码实现

1. IOU概述        IoU的全称为交并比(Intersection over Union),是目标检测中使用的一个概念,IoU计算的是“预测的边框”和“真实的边框”的交叠率-,即它们的交集和并集的比值。最理想情况是完全重叠,即比值为1。 2. IoU计算        IoU等于“预测的边框”和“真实的边框”之间交集和并集的比值。IoU计算如下图,B1为真实

Roaddd的博客 2万+

学习/课设/科研常用小工具Python代码集合(持续更新)

python进行科研课设学习中的小点工具总结。

qq_62711060的博客 370

LVGL在单色屏上运行

本文主要讲述LVGL在单色屏应用中的实现思路,并已验证通过。网上关于lvgl移植的文章有很多了,本文不加以赘述。

weixin_43278295的博客 4474

3分钟极速安装Python:对比5种方法的效率

我的同事或任何感兴趣的人,只需要点开这个链接,就能在网页上直接看到工具运行后的结果——那份对比了五种安装方式的耗时、资源占用的可视化图表报告,无需在他们自己的电脑上安装任何Python环境或依赖库。为了让测试过程更流畅,我将整个流程脚本化:用户只需运行一个主脚本,选择要测试的安装方式(可以多选),工具便会依次执行安装、监控、数据收集,全部完成后自动弹出图表并生成PDF报告。基于测试数据,工具应能自动分析并给出结论,比如“在macOS上,Homebrew是最佳选择”,并支持将报告导出为PDF,方便分享。

BlackironLynx23的博客 29

ZYNQ通过AXI DMA实现PL发送连续大量数据到PS DDR

方案二是在PS端的MPU配置中,将DMA缓冲区所在的内存区域标记为“不缓存”(Device或Strongly Ordered类型),但这样CPU访问这些内存区的速度会大幅降低,需要根据实时性和数据正确性的需求做权衡。更妙的是,描述符链表可以做成一个环状队列,放在DDR里让DMA循环取走,从而实现持续不断的“流式”传输。用FPGA实现FFT硬件加速的系统中也是如此:PS向PL提交待处理的FFT数据,PL硬件加速计算后,DMA把结果写回DDR,全程CPU几乎只做配置调度,大量数据吞吐留给了硬件通道。

QQ_778132974的博客 189

YOLOv11改进策略【Neck】| PRCV 2023,SBA(Selective Boundary Aggregation):特征融合模块,描绘物体轮廓重新校准物体位置,解决边界模糊问题

SBA 模块在医疗图像分割中具有重要作用,其设计出发点是解决图像中物体边界模糊以及融合特征时的冗余和不一致问题。

Limiiiing的博客 1956

fpdf2与其他PDF库对比:为什么它是Python开发者的首选?

在Python的PDF处理领域,开发者常常面临多个库的选择,而**fpdf2**凭借其轻量级设计、强大功能和易用性,正成为越来越多开发者的首选。本文将深入对比fpdf2与其他主流PDF库的核心差异,揭示它如何在性能、功能和开发效率上脱颖而出,帮助你快速掌握这个高效PDF生成工具的独特优势。 ## 🚀 核心优势:为什么选择fpdf2? fpdf2作为FPDF库的现代化分支,在保留简洁API的同

gitblog_00129的博客 597

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

A breakdown pie chart - ReportLab Snippets (Beta)

A breakdown pie chart - ReportLab Snippets (Beta) A breakdown pie chart - ReportLab Snippets (Beta) - ReportLab.comA breakdown pie ch...

94

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

我用 Python 自动生成图文并茂的数据分析报告

reportlab是Python的一个标准库,可以画图、画表格、编辑文字,最后可以输出PDF格式。它的逻辑和编辑一个word文档或者PPT很像。有两种方法:1)建立一个空白文档,然后在上面写文字、画图等;2)建立一个空白list,以填充表格的形式插入各种文本框、图片等,最后生成PDF文档。因为需要产生一份给用户看的报告,里面需要插入图片、表格等,所以采用的是第二种方法。

Python数据挖掘 1862

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

python draw_Python reportlab 之 draw函数介绍

Python reportlab 之 draw函数介绍本节我们讲介绍一下操作canvas时经常用的工具API。在后续教程中我将会对每个工具进行详细介绍,本文把他们都介绍给大家,方便朋友们开发之用。Line methodscanvas.line(x1,y1,x2,y2)canvas.lines(linelist)用line和lines方法可以直接在canvas里绘制直线段Shape methodsc...

weixin_39521068的博客 1524

reportlab 生成pdf文件 (python)

2 应用场景 通过网页动态生成PDF文档 大量的报告和数据发布 用XML一步生成PDF 官网案例 3 PLATYPUS Platypus是“Page Layout and Typography Using Scripts”,是使用脚本的页面布局和印刷术的缩写,这是一个高层次页面布局库,它可以让你通过编程创造复杂的文档,并且毫不费力。Platypus设计的目的是尽可能地将高层布局设计与文档内容分离,比如,段落使用段落格式构造,页面使用页面模板,这样做是有好处的,在仅仅修改几行代码的情况下,包含数百个页面的

消极的人永远是对的,积极的人选择勇往直前 3802

Python reportlab

python强大的PDF处理库reportlab,网上详细的教程比较少,这是我自己看它的user guide,摘选翻译并加入自己的理解写出来

TeamLee的博客 3445

Python利用Reportlab生成PDF文档

通过整整两天时间的学习。已经基本摸清reportlab生成pdf的方法。下面记录我的学习成果: reportlab文档:https://www.reportlab.com/docs/reportlab-userguide.pdf reportlab实例:https://www.programcreek.com/python/index/1920/reportlab.platypus repo...

bocai_xiaodaidai的博客 1万+

python安装reportlab

python3.6安装reportlab的时候报错,错误信息如下: Collecting reportlab Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, ‘[SSL: CERTIFICA...

阳水平的博客 8804
上一篇: Python中的类方法和静态
下一篇: python绘图工具reportlab介绍----续
heybob
博客等级 码龄15年 22粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值