因为工作需要经常需要生成很多的pdf报告给客户查看产品效果以及过程的讲解。
每次都需要按照一定的格式的编写文档并生成pdf报告,这样重复性的工作实在太累。
想着可以使用python生成一份给用户看的报告,里面需要插入图片、表格、文字说明等等。
使用第三方的python非标准模块reportlab就能满足直接生成pdf报告的需要,由于是非标准库需要使用pip的方式安装一下该模块。
使用pip安装reportlab模块,支持生成pdf文档。
pip install reportlab -i https://pypi.tuna.tsinghua.edu.cn/simple
若是在安装过程中出现缺失c++环境导致构建失败时,可以直接选择使用wheel文件的方式安装reportlab模块。
.whl文件的下载地址如下:https://www.lfd.uci.edu/~gohlke/pythonlibs/
下载完成之后存储到本地磁盘,按照存放的路径安装reportlab模块即可,安装方式可以参考下面的安装方式。
pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simplepip install d:\downloads\reportlab-3.5.57-cp36-cp36m-win_amd64.whl
提前将reportlab模块中需要使用到的python对象导入到当前的代码块中。
from reportlab.pdfbase import pdfmetrics # 注册字体from reportlab.pdfbase.ttfonts import ttfont # 字体类from reportlab.platypus import table, simpledoctemplate, paragraph, image # 报告内容相关类from reportlab.lib.pagesizes import letter # 页面的标志尺寸(8.5*inch, 11*inch)from reportlab.lib.styles import getsamplestylesheet # 文本样式from reportlab.lib import colors # 颜色模块from reportlab.lib.units import cm # 单位:cm
模块导入完成之后,第一步需要设置pdf文档中使用到的字体,字体可以根据自己的喜好自行设置。
# registering a font named 'simfang' with the file 'simfang.ttf'.pdfmetrics.registerfont(ttfont('simfang', 'simfang.ttf'))
我这里选择的字体是simfang.ttf,关于windows系统中的默认字体可以下面的路径中查看。
开发业务代码之前,我们可以将公共的部分提到最外面,这里使用getsamplestylesheet函数将获取到所有的样式表后面在其他地方也可以使用。
# getting a list of styles that can be used in the document.style_list = getsamplestylesheet()
1、插入pdf标题大标题设置字体样式对象为heading1,字体颜色为绿色,大小为18并且加粗。
def insert_full_title(title_name=none): """ this function takes in a title name and returns the full title name. :param title_name: the name of the title you want to insert """ font_ = style_list['heading1'] font_.fontname = 'simfang' font_.fontsize = 18 font_.leading = 50 font_.textcolor = colors.green font_.alignment = 1 font_.bold = true return paragraph(title_name, font_)
2、插入pdf小标题小标题设置字体样式对象为normal,字体颜色为红色,大小为15并且不加粗。
def insert_lettle_title(lettle_name=none): """ :param lettle_name: the name of the lettle you want to insert """ font_ = style_list['normal'] font_.fontname = 'simfang' font_.fontsize = 15 font_.leading = 30 font_.textcolor = colors.red return paragraph(lettle_name, font_)
3、插入普通段落文本普通文本设置字体样式对象为normal,字体颜色为默认,大小为12并且不加粗,开启自动换行模式。
def insert_text(text=none): """ > this function inserts text into the current document :param text: the text to insert """ font_ = style_list['normal'] font_.fontname = 'simfang' font_.fontsize = 12 font_.wordwrap = 'cjk' font_.alignment = 0 font_.firstlineindent = 32 font_.leading = 25 return paragraph(text, font_)
4、插入pdf图片将图片插入到pdf文档对象中比较简单,只需要设置需要插入图片的本地路径即可。
def insert_image(image_path=none): """ > this function inserts an image into the notebook :param image_path: the path to the image you want to insert """ img = image(image_path) img.drawwidth = 5 * cm img.drawheight = 8 * cm return img
5、插入pdf表格插入表格时,表格的格式可以根据自己的喜好设置表格的标题、字体样式、字体大小以及是否需要合并等参数来控制需要插入的表格对象。
def insert_table(*args): """ it inserts a table into the database. """ col_width = 120 style = [ ('fontname', (0, 0), (-1, -1), 'simfang'), # 字体 ('fontsize', (0, 0), (-1, 0), 12), # 第一行的字体大小 ('fontsize', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小 ('background', (0, 0), (-1, 0), '#d5dae6'), # 设置第一行背景颜色 ('align', (0, 0), (-1, -1), 'center'), # 第一行水平居中 ('align', (0, 1), (-1, -1), 'left'), # 第二行到最后一行左右左对齐 ('valign', (0, 0), (-1, -1), 'middle'), # 所有表格上下居中对齐 ('textcolor', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色 ('grid', (0, 0), (-1, -1), 0.5, colors.grey), # 设置表格框线为grey色,线宽为0.5 ] table = table(args, colwidths=col_width, style=style) return table
上述就是pdf文档中常用的对象,最后通过添加对应的内容参数即可生成pdf文档并保存到本地磁盘当中。
# a special variable in python that evaluates to `true` if the module is being run as the main program.if __name__ == '__main__': pdf_ = list() pdf_.append(insert_full_title('数据测试报告')) pdf_.append(insert_text( 'python 是一门编程语言。 您可以在服务器上使用 python 来创建 web 应用程序。通过实例学习 我们的 tiy 编辑器使学习 python 变得简单,它能够同时显示代码和结果。 ')) pdf_.append(insert_image('./excle源数据.png')) pdf_.append(insert_lettle_title('数据内容展示:')) data = [ ('职位名称', '平均薪资', '较上年增长率'), ('数据分析师', '18.5k', '25%'), ('高级数据分析师', '25.5k', '14%'), ('资深数据分析师', '29.3k', '10%') ] pdf_.append(insert_table(*data)) doc = simpledoctemplate('测试报告.pdf', pagesize=letter) doc.build(pdf_)
以上就是如何使用python自动化生成pdf报告?的详细内容。