将绘图添加到演示文稿中,而不将其保存为图像



我正在尝试将图像添加到powerpoint演示文稿中。我能够实现它,但我想添加它而不将其保存到资源管理器。

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pptx import Presentation
from pptx.util import Inches
np.random.seed(12)
df = pd.DataFrame(np.random.randn(6, 4), index = range(1, 7), columns = list('ABCD'))
# creating and saving the plot in the explorer
plot_1 = plt.plot(df)
plt.savefig('plot_1.png')
plot_1 = 'plot_1.png'
presentation = Presentation()
presentation.slide_width = Inches(16)
presentation.slide_height = Inches(9)
layout = presentation.slide_layouts[6]
slide = presentation.slides.add_slide(layout)
left = Inches(1)
top = Inches(0.5)
plot = slide.shapes.add_picture(plot_1, left, top)
presentation.save('test.pptx')

任何想法如何添加情节到演示文稿而不保存到资源管理器?

可以使用"add_chart"python-pptx提供的函数。它将创建交互式图表,就像你从Powerpoint中创建一样。官方文档链接

chart_data = ChartData()
chart_data.categories = 'Foo', 'Bar'
chart_data.add_series('Series 1', (1.2, 2.3))
chart_data.add_series('Series 2', (3.4, 4.5))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4)
graphic_frame = shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
chart = graphicFrame.chart

最新更新