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

Read more

使用 VS Code 连接 MySQL 数据库

使用 VS Code 连接 MySQL 数据库

文章目录 * 前言 * VS Code下载安装 * 如何在VS Code上连接MySQL数据库 * 1、打开扩展 * 2、安装MySQL插件 * 3、连接 * 导入和导出表结构和数据 前言 提示:这里可以添加本文要记录的大概内容: 听说VS Code不要钱,功能还和 Navicat 差不多,还能在上面打游戏 但是没安装插件是不行的 发现一个非常牛的博主 还有一个非常牛的大佬 提示:以下是本篇文章正文内容,下面案例可供参考 VS Code下载安装 VS Code下载安装 如何在VS Code上连接MySQL数据库 本篇分享是在已有VS Code这个软件的基础上,数据库举的例子是MySQL 1、打开扩展 2、安装MySQL插件 在搜索框搜索 MySQL和 MySQL Syntax,下载这三个插件 点击下面的插件,选择【install】安装

By
RustFS 保姆级上手指南:国产开源高性能对象存储

RustFS 保姆级上手指南:国产开源高性能对象存储

最近在给项目选型对象存储的时候,发现一个挺有意思的现象:一边是MinIO社区版功能逐渐“躺平”,另一边是大家对存储性能和安全性的要求越来越高。就在这时,一个叫 RustFS 的国产开源项目闯入了我的视野。 折腾了一阵子后,我感觉这玩意儿确实有点东西。它用Rust语言写,天生就带着高性能和内存安全的基因,性能号称比MinIO快一大截,而且用的是对商业友好的Apache 2.0协议。今天,我就手把手带大家从零开始,搭建一个属于自己的RustFS服务,体验一下国产存储的威力。 一、 RustFS是什么?为什么值得你关注? 简单说,RustFS是一个 分布式对象存储系统 。你可以把它理解成一个你自己搭建的、功能跟阿里云OSS、亚马逊S3几乎一样的“私有云盘”。 但它有几个非常突出的亮点,让我觉得必须试试: * 性能猛兽 :基于Rust语言开发,没有GC(垃圾回收)带来的性能抖动,官方数据显示在4K随机读场景下,性能比MinIO高出40%以上,内存占用还不到100MB,简直是“小钢炮”。 * 100%S3兼容 :这意味着你现有的所有使用S3 API的代码、工具(比如AWS

By