有没有办法用python-pptx在powerpoint演示文稿中绘制matplotlib图表,而不用将图表保存为*.jpg或*.png?下面是一种简单的方法,将matplotlib图形保存为图像文件,然后将其加载到python-pptx,但这根本不是一种有效的方法。
import numpy as np
import matplotlib.pyplot as plt
from pptx import Presentation
from pptx.util import Inches
np.random.seed(5)
x = np.arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
plt.plot(x, y, "o")
plt.plot([70, 70], [100, 250], 'k-', lw=2)
plt.plot([70, 90], [90, 200], 'k-')
plt.show()
plt.savefig('graph.jpg')
img = 'graph.jpg'
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
pic = slide.shapes.add_picture(img, Inches(1), Inches(1), height=Inches(1))发布于 2017-05-10 05:01:57
绘图可以保存为内存中的类文件对象(StringIO),然后传递给python-pptx
from StringIO import StringIO
image_stream = StringIO()
plt.savefig(image_stream)
pic = shapes.add_picture(image_stream, x, y, cx, cy)https://stackoverflow.com/questions/43875424
复制相似问题