I would like to add a picture into a content placeholder in pptx using python. I'm forced using the company template, which uses the content placeholders for some pictures. How do I add a figure into a content placeholder?
Is there an attribute in the content placeholder object, to insert a picture? I have 'guessed' one, which does not give an error but also does not import a figure (see below).
As a workaround, is there a simple way to replace the content placeholder with a picture placeholder, keeping all dimensions and positioning the same?
from pptx import Presentation
prs = Presentation('blank_template.pptx')
################
# Currently only possible to append, not insert halfway the presentation for instance. Thus removal of slides is necessary.
#####
# Layout numbers:
#
# 0 Title slide
# 1 Title and content
# 9 Section header
# 16 Comparison
# 19 Title and 4 pictures
#
########################
#
# Placeholders
#
# layout 1
# 0 Title 1
# 1 Content Placeholder 2
#
# layout 9
# 0 Title 1
# 1 Text Placeholder 2
#
# layout 16
# 0 Title 1
# 1 Text Placeholder 2
# 2 Content Placeholder 3
# 3 Text Placeholder 4
# 4 Content Placeholder 5
#
# layout 19
# 0 Title 1
# 1 Picture Placeholder 2
# 13 Picture Placeholder 3
# 14 Picture Placeholder 4
# 15 Picture Placeholder 5
# 16 Text Placeholder 6
# 17 Text Placeholder 7
# 18 Text Placeholder 8
# 19 Text Placeholder 9
#
##########################
#table of content
Header1='First content'
Header2='Second content'
#Slide 1;
#table of content
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
title_placeholder.text = 'Table of Content'
placeholder = slide.placeholders[1] # idx key, not position
placeholder.text = Header1
#Slide 2;
#header 1
slide_layout = prs.slide_layouts[9]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
title_placeholder.text = Header1
#Slide 3
#text block
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
title_placeholder.text = Header1+' - text block'
#Slide 4
#comparison figure
slide_layout = prs.slide_layouts[16]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
title_placeholder.text = Header1+' - comparison'
placeholder = slide.placeholders[1] # idx key, not position
placeholder.text = 'image 1'
placeholder = slide.placeholders[2] # idx key, not position
image= 'my-image.png'
placeholder.picture= image
placeholder = slide.placeholders[3] # idx key, not position
placeholder.text = 'image 2'
#placeholder = slide.placeholders[4] # idx key, not position
#picture = placeholder.insert_picture('my-image.png')
#slide 5
#4 pictures
slide_layout = prs.slide_layouts[19]
slide = prs.slides.add_slide(slide_layout)
title_placeholder = slide.shapes.title
title_placeholder.text = Header1+' - 4 pictures'
placeholder = slide.placeholders[1] # idx key, not position
picture = placeholder.insert_picture('my-image.png')
placeholder = slide.placeholders[13] # idx key, not position
picture = placeholder.insert_picture('my-image.png')
placeholder = slide.placeholders[14] # idx key, not position
picture = placeholder.insert_picture('my-image.png')
placeholder = slide.placeholders[15] # idx key, not position
picture = placeholder.insert_picture('my-image.png')
prs.save('updated.pptx')