【发布时间】:2018-05-25 20:15:21
【问题描述】:
目前我正在尝试绘制我的 DataFrame 数据并添加到我的 pptx 幻灯片中。我使用 matplotlib + pptx-python 来完成这项工作。在此过程中,我尝试将绘图图像保存到 io 内存流并将其用于 pptx 幻灯片。步骤是:
import io
from PIL import Image
from pptx import Presentation
from pptx.util import Inches
#1. Run Python-pptx and open presentation
prs = Presentation('Template.pptx')
title_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
title.text = 'FileName'
left = top = Inches(1)
#2. plot the data in matplotlib
fig, ax = plt.subplots()
df.groupby('Name').plot(x='Time',y='Score', ax=ax)
#3. save the plot to io in-memory stream
buf = io.BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
im = Image.open(buf)
#4. add image to slide
pic = slide.shapes.add_picture(im, left, top)
但是我收到了这样的错误:
AttributeError: 'PngImageFile' object has no attribute 'read'
你知道如何解决这个问题吗?顺便说一句,我正在使用 Python 3.6。我尝试更新 PIL 包并为我的图像使用“png”和“jpg”格式。所有的努力都没有奏效。
【问题讨论】:
-
你试过
format='jpg')或format='png')吗? -
我尝试更改格式并更新 PIL 包。但一切都不起作用。
标签: python python-3.x io python-pptx