I want to insert the plot I created into the power point with insert_picture()
.
I searched from online and found info here.
I create a simple figure as below:
fig, ax = plt.subplots(2,1, sharex=True, sharey=True)
And I use python-pptx
to create a power point.
graph_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(graph_slide_layout)
title = slide.shapes.title
title.text = "Test"
placeholder = slide.placeholders[1]
pic = placeholder.insert_picture(fig)
prs.save('Desktop\test.pptx')
However, I got an error:
AttributeError: 'SlidePlaceholder' object has no attribute 'insert_picture'
There is a note I found here.
Note
A reference to a picture placeholder becomes invalid after its insert_picture() method is called.
This is because the process of inserting a picture replaces the original p:sp XML element with a new p:pic element containing the picture.
Any attempt to use the original placeholder reference after the call will raise AttributeError.
The new placeholder is the return value of the insert_picture() call and may also be obtained from the placeholders collection using the same idx key.
After that, I changed the code to:
graph_slide_layout = prs.slide_layouts[8]
slide = prs.slides.add_slide(graph_slide_layout)
title = slide.shapes.title
title.text = "Test"
placeholder = slide.placeholders[1]
pic = placeholder.insert_picture(fig)
prs.save('Desktop\test.pptx')
placeholder.name
'Picture Placeholder 2'
placeholder.placeholder_format.type
18
I got another error:
AttributeError: 'Figure' object has no attribute 'seek'
Any idea?