用python生成pdf report

1. 安装

pip install pdfkit

pip install Jinja2

同时安装 wkhtmltopdf;

https://towardsdatascience.com/creating-pdf-reports-with-python-pdfkit-and-jinja2-templates-64a89158fa2d

2. 使用Plotly + Jupyter notebook + xhtml2pdf生成pdf report

https://plotly.com/python/v3/pdf-reports/

3. 利用pdf_reports包生成pdf,

https://github.com/Edinburgh-Genome-Foundry/pdf_reports

上述方法都有一个特点,需要借助一定的HTML代码生成最后的pdf文档。

目前没有找到类似于R语言中的Rmarkdown方便生成pdf文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python 生成 PDF 文件,可以使用 ReportLab 库。而要在 PDF 文件中加入标题、副标题、折线图、柱状图和饼图,则需要使用其他库来生成这些图形,比如 Matplotlib 和 Seaborn。 以下是一份简单的示例代码,它使用 ReportLab、Matplotlib 和 Seaborn 来生成包含标题、副标题、折线图、柱状图和饼图的 PDF 文件: ```python from reportlab.lib.enums import TA_CENTER from reportlab.lib.pagesizes import letter, landscape from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import inch from reportlab.pdfgen.canvas import Canvas from reportlab.platypus import Paragraph, SimpleDocTemplate import matplotlib.pyplot as plt import seaborn as sns # Generate data for the charts x = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] y2 = [5, 3, 8, 1, 6] labels = ['A', 'B', 'C', 'D', 'E'] sizes = [20, 30, 40, 5, 5] # Generate the charts fig, ax = plt.subplots() ax.plot(x, y1, label='Line 1') ax.plot(x, y2, label='Line 2') ax.legend() plt.savefig('line_chart.png') fig, ax = plt.subplots() ax.bar(x, y1, label='Bar 1') ax.bar(x, y2, bottom=y1, label='Bar 2') ax.legend() plt.savefig('bar_chart.png') plt.figure() plt.pie(sizes, labels=labels) plt.axis('equal') plt.savefig('pie_chart.png') # Generate the PDF doc = SimpleDocTemplate('report.pdf', pagesize=landscape(letter)) styles = getSampleStyleSheet() title_style = ParagraphStyle(name='Title', fontSize=20, alignment=TA_CENTER) subtitle_style = ParagraphStyle(name='Subtitle', fontSize=16, alignment=TA_CENTER) # Add the title title = Paragraph('Report Title', title_style) doc.build([title]) # Add the subtitle subtitle = Paragraph('Report Subtitle', subtitle_style) doc.build([subtitle]) # Add the line chart canvas = Canvas('report.pdf', pagesize=landscape(letter)) canvas.drawImage('line_chart.png', inch, inch, width=6*inch, height=4*inch) canvas.showPage() # Add the bar chart canvas.drawImage('bar_chart.png', inch, inch, width=6*inch, height=4*inch) canvas.showPage() # Add the pie chart canvas.drawImage('pie_chart.png', inch, inch, width=6*inch, height=4*inch) canvas.showPage() canvas.save() ``` 这份示例代码会生成一个名为 `report.pdf` 的 PDF 文件,其中包含了一个标题、一个副标题、一个折线图、一个柱状图和一个饼图。请注意,这份代码可能需要根据你的具体需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值