0

I am creating a powerpoint in Python, where the slides are the output graphs from the Jupyter Notebook cells.

I am able to do this, but only if I save each output as a jpg. For example, see the code below:

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"

Is there a way of creating a similar line toplt.savefig("Total.jpg",bbox_inches='tight'), without saving a file - i.e calling the plot "something (say x)" that can be called back at any point? Then later I can replace the line img1 = 'Total.jpg' with img1 = x etc.

MRHarv
  • 491
  • 1
  • 10
  • 22
  • You can save the image to a buffer. The supply that buffer to `add_picture`. – ImportanceOfBeingErnest Feb 15 '19 at 14:46
  • The "buffer" would be a file-like object like `StringIO` or perhaps `BytesIO` depending on your Python version. These behave like "in-memory" files. https://docs.python.org/2/library/stringio.html – scanny Feb 15 '19 at 19:07

0 Answers0