Python 自动生成 PDF 报告:ReportLab 实战教程(含中文字体支持)

在数据报告、发票生成、证书制作等应用中,自动生成 PDF 是常见需求。本文将带你用 Python 的 reportlab 库生成专业的 PDF 报表,支持文字排版、表格、图表,并解决中文乱码问题。


一、安装 ReportLab


bash

复制编辑

pip install reportlab

安装成功后可直接使用 reportlab.pdfgenreportlab.platypus 模块。


二、创建第一个 PDF 文件


python

复制编辑

from reportlab.pdfgen import canvas c = canvas.Canvas("demo.pdf") c.drawString(100, 750, "Hello, PDF!") c.save()

生成后的 PDF 文件位于当前目录,文字位于页面左下角 (100, 750) 坐标。


三、设置字体与样式(含中文支持)

ReportLab 默认不支持中文,我们需要手动注册中文字体。

步骤 1:下载字体(如 SimSun.ttf 或微软雅黑)

步骤 2:注册中文字体


python

复制编辑

from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf'))

步骤 3:使用中文字体绘制文字


python

复制编辑

c = canvas.Canvas("chinese.pdf") c.setFont("SimSun", 14) c.drawString(100, 750, "你好,PDF 世界!") c.save()


四、使用 Platypus 创建结构化报告

Platypus 是 reportlab 高级布局引擎,支持段落、表格、图像、分页等。


python

复制编辑

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet doc = SimpleDocTemplate("report.pdf") styles = getSampleStyleSheet() story = [] story.append(Paragraph("Python 自动生成 PDF 报告示例", styles["Title"])) story.append(Spacer(1, 20)) story.append(Paragraph("这是一段正文内容,可以自动换行,并支持丰富的样式设置。", styles["Normal"])) doc.build(story)


五、添加表格与图表

插入表格


python

复制编辑

from reportlab.platypus import Table, TableStyle from reportlab.lib import colors data = [["姓名", "成绩"], ["张三", 90], ["李四", 85]] table = Table(data) table.setStyle(TableStyle([ ("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey), ("GRID", (0, 0), (-1, -1), 1, colors.black), ("ALIGN", (0, 0), (-1, -1), "CENTER"), ])) story.append(table)


六、插入图片和分页控制


python

复制编辑

from reportlab.platypus import Image, PageBreak img = Image("chart.png", width=300, height=200) story.append(Spacer(1, 20)) story.append(img) story.append(PageBreak()) # 换页


七、批量生成 PDF 报告(如:学生成绩单)


python

复制编辑

students = [ {"name": "张三", "score": 92}, {"name": "李四", "score": 88}, ] for stu in students: doc = SimpleDocTemplate(f"{stu['name']}_成绩单.pdf") content = [ Paragraph(f"{stu['name']} 的成绩单", styles["Title"]), Spacer(1, 20), Paragraph(f"总分:{stu['score']}", styles["Normal"]) ] doc.build(content)


八、总结

功能是否支持
文字排版
中文字体支持✅(需注册)
表格与图表
分页与多页报告
复杂文档结构

如果你正在开发自动报告系统、发票模块或证书生成工具,reportlab 是 Python 中最专业的 PDF 生成方案之一。

https://bigu.wang

https://www.bigu.wang

https://binm.wang

https://www.binm.wang

https://bint.wang

https://www.bint.wang

https://biop.wang

https://www.biop.wang

https://bits.wang

https://www.bits.wang

https://bjqb.wang

https://www.bjqb.wang

https://bjsm.wang

https://www.bjsm.wang

https://bleo.wang

https://www.bleo.wang

https://ono.wang

https://www.ono.wang

https://onz.wang

https://www.onz.wang

https://opo.wang

https://www.opo.wang

https://osm.wang

https://www.osm.wang

https://osn.wang

https://www.osn.wang

https://ovi.wang

https://www.ovi.wang

https://oxq.wang

https://www.oxq.wang

https://oti.wang

https://www.oti.wang

https://owu.wang

https://www.owu.wang

https://piq.wang

https://www.piq.wang

https://qmi.wang

https://www.qmi.wang

https://qki.wang

https://www.qki.wang

https://ref.wang

https://www.ref.wang

https://sak.wang

https://www.sak.wang

https://sar.wang

https://www.sar.wang

https://sfa.wang

https://www.sfa.wang

https://sfe.wang

https://www.sfe.wang

https://sgo.wang

https://www.sgo.wang

https://sku.wang

https://www.sku.wang

https://ycxjz.cn

https://www.ycxjz.cn

https://bnbmhomes.cn

https://www.bnbmhomes.cn

https://jinjianzuche.com

https://www.jinjianzuche.com

https://ahswt.cn

https://www.ahswt.cn

https://szwandaj.cn

https://www.szwandaj.cn

https://psbest.cn

https://www.psbest.cn

https://shanghai-arnold.cn

https://www.shanghai-arnold.cn

https://zgsscw.com

https://www.zgsscw.com

https://shxqth.cn

https://www.shxqth.cn

https://wdxj.cn

https://www.wdxj.cn

https://jad168.com

https://www.jad168.com

https://ultratrailms.cn

https://www.ultratrailms.cn

https://tztsjd.cn

https://www.tztsjd.cn

https://csqcbx.cn

https://www.csqcbx.cn

https://qazit.cn

https://www.qazit.cn

https://ahzjyl.cn

https://www.ahzjyl.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值