我正在用Python创建一个PowerPoint,幻灯片是Jupyter Notebook单元的输出图。
我能够执行此操作,但前提是我将每个输出另存为jpg。例如,请参见下面的代码:
data = pd.read_csv("123.csv")
data.columns = ['Color Group', 'BASE VOLUME']
x= data['BASE VOLUME']
y= data['Color Group']
data2 = data
data2['BASE VOLUME %'] = data2['BASE VOLUME']
data2 = data2.iloc[:,[0,2]]
data2['BASE VOLUME %'] = 100*data2['BASE VOLUME %']/(sum(data2['BASE VOLUME %']))
data2['Type'] = 'Total'
total = data2.copy()
plt.figure(figsize=(10,6))
clrs = ['deepskyblue' if (x > 10) else 'gray' for x in data2['BASE VOLUME %']]
ax = sns.barplot(x,y, data=data2, palette=clrs)
ax.set_xlabel('Base Volume',fontsize=15)
ax.set_ylabel('Color Group',fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
for i, v in enumerate(data2['BASE VOLUME %']):
ax.text(0 + 0.01*max(data['BASE VOLUME']), i + len(data2)/50, str("{0:.1f}%".format(v)), color='black', fontweight='bold', fontsize=14)
plt.title('Total'.format(s),fontsize=15)
plt.savefig("Total.jpg",bbox_inches='tight')
#Create Powerpoint
from pptx import Presentation
from pptx.util import Inches,Pt
prs = Presentation('input.pptx')
blank_slide_layout = prs.slide_layouts[1]
img1 = 'Total.jpg'
slide = prs.slides.add_slide(blank_slide_layout)
pic = slide.shapes.add_picture(img1, Inches(0.55), Inches(2), height=Inches(4.8), width=Inches(8.8))
title = slide.shapes.title
title.text_frame.paragraphs[0].font.size=Pt(50)
title.text = "Total"
有没有一种方法可以创建与plt.savefig("Total.jpg",bbox_inches='tight')
类似的行,而又不保存文件-即称该情节为“某物(例如x)”,可以随时对其进行回调?然后,我可以将img1 = 'Total.jpg'
行替换为img1 = x
等。