How to input graph from dataframe into pdf using reportlab?

问题描述 投票:0回答:0

我在后台使用具有 python 脚本功能的在线程序 - 所以我能做的事情受到限制。

我正在尝试绘制一个基于数据框绘制的图,并使用 reportlab 将该图输入到 pdf 中。该报告生成后,需要输出一个可点击的链接以下载pdf。

下面的代码给出了一个空白的 pdf,我不明白为什么 - 我已经在这段代码上工作了 3 天,我快要疯了。

from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Image
import base64
from IPython.display import HTML, display

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

#Find durations
start = 0
start_i = 0
end_i = 1

# create a buffer to hold the generated PDF
buffer = BytesIO()

# create a blank story 
elements = []

# create a PDF object
pdf = SimpleDocTemplate(buffer, pagesize=letter)

# create a plot from the dataframe
#fig, ax = plt.subplots(figsize=(11.69,8.27))
product_fig, ax = plt. subplots(dpi = 300)
product_df = (DataFrame)

product_df.plot()
product_fig.savefig("./fig", dpi=300)

# add the plot to the PDF
elements.append(Image("./fig"))

# reset the buffer position to the beginning
buffer.seek(0)

# encode the PDF in base64 for download
b64 = base64.b64encode(buffer.getvalue()).decode()

# create a link to download the PDF file
link = '<a href="data:application/pdf;base64,{0}" download="dataframe.pdf">Right-click -> Save As to Download PDF</a>'.format(b64)

# display the link
display(HTML(link))
python pdf reportlab
© www.soinside.com 2019 - 2024. All rights reserved.