gpt4 book ai didi

python - 如何将 Matplotlib.pyplot 中的绘图插入到 FPDF 文档中?

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:20 24 4
gpt4 key购买 nike

我正在尝试制作一个小(非常简单)的脚本来生成统计报告。该报告应包含一些文字、一些书面统计数据(数字)和一些来自 Matplotlib 的绘图。

我听说 FPDF 是创建 PDF 文档的最佳库之一

但是如何将绘图插入到 FPDF 文档中呢?

每当我尝试从 matplotlib FPDF 导入 PNG 时,都会简单地说:

运行时错误:FPDF 错误:不支持的图像类型:fig_img1

最佳答案

  1. 您可以将图形另存为图片,然后使用 FPDF 在 PDF 中插入图片。这相当简单,您可以轻松查找。

  2. 另一种方法是将图像保存到 bytesIO 对象,而不显式将其转储为 png 文件,然后获取 FPDF 对象来读取该图像。 fpdf 当前不支持此功能,因此您需要自己实现此功能。

为此,请按顺序查阅以下三个答案:

Writing matplotlib figure to a buffer, then reading as a file for embedding an image in a PDF

Insert Base64 image to pdf using pyfpdf

load_resource function not found as a class method of FPDF

关于python - 如何将 Matplotlib.pyplot 中的绘图插入到 FPDF 文档中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58015710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com