Inserting an Image (that fits) in Powerpoint with Python

In this code you can see an example to insert an image in Powerpoint with Python and the module python-pptx.

The python pptx module can be seen through this scheme:

Old code may not work with python 3.10

The code below may not work anymore

from pptx import Presentation
import os

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
placeholder = slide.placeholders[1]
picture = placeholder.insert_picture('001.png')
prs.save("ESEMPIO.pptx")
os.startfile("ESEMPIO.pptx")

Workaround to AttributeError: module ‘collections’ has no attribute ‘Container’

With python 3.10, this code does not work. There is a workaround as you can see here.

import collections 
import collections.abc
from pptx import Presentation
import os

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
placeholder = slide.placeholders[1]
picture = placeholder.insert_picture('001.png')
prs.save("ESEMPIO.pptx")
os.startfile("ESEMPIO.pptx")

Now it should work.

The code that fits the image in the placeholder

This is the code to add to make the image fit.

And this is the code to make the slides with the _add_image that uses the code above, replacing the slide.splaceholders[1].add_picture(‘…png’) stuff.

from pptx import Presentation
import os
from PIL import Image

# pip install python-pttx
# thanks to Thomas Winters for this
def _add_image(slide, placeholder_id, image_url):
    placeholder = slide.placeholders[placeholder_id]

    # Calculate the image size of the image
    im = Image.open(image_url)
    width, height = im.size

    # Make sure the placeholder doesn't zoom in
    placeholder.height = height
    placeholder.width = width

    # Insert the picture
    placeholder = placeholder.insert_picture(image_url)

    # Calculate ratios and compare
    image_ratio = width / height
    placeholder_ratio = placeholder.width / placeholder.height
    ratio_difference = placeholder_ratio - image_ratio

    # Placeholder width too wide:
    if ratio_difference > 0:
        difference_on_each_side = ratio_difference / 2
        placeholder.crop_left = -difference_on_each_side
        placeholder.crop_right = -difference_on_each_side
    # Placeholder height too high
    else:
        difference_on_each_side = -ratio_difference / 2
        placeholder.crop_bottom = -difference_on_each_side
        placeholder.crop_top = -difference_on_each_side


prs = Presentation()

layout8 = prs.slide_layouts[8]
slide = prs.slides.add_slide(layout8)

title = slide.shapes.title.text = "This is Powerpoint"
sub = slide.placeholders[2].text = "Python has the power"
_add_image(slide,1,"003.png")

prs.save("MyPresentation.pptx")
os.startfile("MyPresentation.pptx")

Here is the whole code

Look at this code related to python and powerpoint

This example let you insert text and images in powerpoint using python in a very neat way. Click here or on the imagine below:

Python and Powerpoint


Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.