【发布时间】:2018-11-11 03:17:08
【问题描述】:
我在 pandas 中生成了一些图并将其保存在 BytesIO 流中,然后我想将其添加到 pdf 页面,然后将 pdf 文件作为附件发送到电子邮件中:
import matplotlib.pyplot as plt
import io
from fpdf import FPDF
fig = plt.figure()
...
buf = io.BytesIO()
fig.savefig(buf, format='png')
pdf = FPDF()
pdf.add_page()
pdf.image(buf.getvalue(), type='PNG')
buf.close()
但这不起作用,报告以下错误:
Traceback (most recent call last):
File "XXXX.py", line 166, in send_email
pdf.image(buf.getvalue(), type='PNG')
File "/usr/local/lib/python3.6/site-packages/fpdf/fpdf.py", line 150, in wrapper
return fn(self, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/fpdf/fpdf.py", line 971, in image
info=self._parsepng(name)
File "/usr/local/lib/python3.6/site-packages/fpdf/fpdf.py", line 1769, in _parsepng
if name.startswith("http://") or name.startswith("https://"):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
我想纯粹在内存中解决这个问题,而不是在本地保存图像文件。谁能帮我这个?非常感谢。
【问题讨论】:
-
image 方法需要一个“名称”(文件名或 URL),而不是
bytes。
标签: python-3.x pandas matplotlib io fpdf