利用reportlab库生成PDF文档,真香

有时候,我们需要把当前的资料转为pdf格式,进行传输发送 。现在转换pdf格式的工具有很多,但是,如何通过代码来实现呢?
我们使用reportlab这个强大的工具直接生成pdf文件 。


reportlab库

reportlab库是用python语言生成pdf文件的模块 。该模块功能非常强大 。
reportLab库根据图形命令直接创建PDF 。生成PDF文档的速度非常快,有时比传统的工具快几个数量级 。reportLab库的不同之处在于它可以在更高的级别,带有一个功能齐全的引擎,用于配置带有表格和图表的文档 。


reportLab库的使用场景:
?在网络上动态生成PDF
?大量数据库信息发布
?其他应用程序的调用
?跨平台操作
repostlab有一个方法手册,分为12大类,非常详细 。这个资料很难获取,如果大家需要的话,站内私信我 。
利用reportlab库生成PDF文档,真香

安装:pip install reportlab
该库默认不支持中文,如果使用中文需要注册
通过代码来演示这个模块的功能 。
from reportlab.pdfbase import pdfmetrics


from reportlab.platypus import Table, SimpleDocTemplate, Paragraph
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle


from reportlab.lib import colors
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.graphics.shapes import Drawing


from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.charts.legends import Legend


# 注册字体
pdfmetrics.registerFont(TTFont('STSONG', 'STSONG.TTF')) #register Font
pdfmetrics.registerFont(TTFont('simhei', 'simhei.ttf')) #register Font
#定义样式
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(fontName='STSONG', name='STSONG', leading=20, fontSize=12, firstLineIndent=22, wordWrap='CJK'))
styles.add(ParagraphStyle(fontName='simhei', name='simhei', leading=25, fontSize=14, wordWrap='CJK')) # content Font
#画出标题
def draw_title():
style = getSampleStyleSheet()
ct = style['Normal']
ct.fontName = 'simhei'
ct.fontSize = 18
# 设置行距
ct.leading = 50
# 颜色
ct.textColor = colors.green
# 居中
ct.alignment = 1
# 添加标题并居中
title = Paragraph('reportlab模块介绍', ct)
return title


def draw_content():
#使用定义好的样式styles['simhei']
str='''
reportlab模块是用python语言生成pdf文件的模块 。该模块功能非常强大 。
reportLab模块根据图形命令直接创建PDF 。生成PDF文档的速度非常快,有时比传统的工具快几个数量级 。
reportLab库的不同之处在于它可以在更高的级别 , 带有一个功能齐全的引擎,用于配置带有表格和图表的文档 。
'''
text = Paragraph(str, styles['simhei'])
return text
# 绘制表格
def draw_table(*args):
col_width = 60
。。。。。。
return table


#创建图表
def draw_bar(bar_data=https://www.itzhengshu.com/pdf/[], ax=[], items=[]):
drawing = Drawing(500, 250)
。。。。。。
return drawing


if __name__ == "__main__":
content =[]
# 添加标题
content.append(draw_title())
# # 添加段落
content.append(draw_content())
# # 添加表格数据
data = https://www.itzhengshu.com/pdf/[('销售业绩', '2020-1', '2020-2', '2020-3', '2020-4', '2020-5'),
('张三', 30, 80, 60, 35, 90),
('李四', 22, 33, 11, 45, 60),
('王五', 20, 10, 15, 80, 50)]
content.append(draw_table(*data))
# 添加图表
b_data = https://www.itzhengshu.com/pdf/[(30, 80, 60, 35, 90), (22, 33, 11, 45, 60), (20, 10, 15, 80, 50)]
ax_data = https://www.itzhengshu.com/pdf/['2020年1月', '2020年2月', '2020年3月', '2020年4月', '2020年5月']
leg_items = [(colors.red, '张三'), (colors.green, '李四'), (colors.blue, '王五')]
content.append(draw_bar(b_data, ax_data, leg_items))
# 生成pdf文件
doc = SimpleDocTemplate('report0.pdf')
doc.build(content)
代码执行结果如下 。
利用reportlab库生成PDF文档,真香

上述代码虽然多,但是不复杂,以函数方式提供 , 方便调用 。
专业使用pdf、跨平台操作,使用reportlab这个专业库 。
【利用reportlab库生成PDF文档,真香】代码有点长,系统提示文字过多,无法发布,需要代码的站内私信我 。

相关经验推荐