Python-pptx: put images in powerpoint

In this simple script you can see how tp insert a image in powerpoint. We used the layout n.3, but you can change it in the list containing the data.

It may not work since python 3.10

This code may not work for the new version of python. In this case there is another example after this that it should work.

from pptx import Presentation
import os

prs = Presentation()

class MySlide:
    def __init__(self, data):
        self.layout = prs.slide_layouts[data[3]]
        self.slide=prs.slides.add_slide(self.layout)
        self.title=self.slide.shapes.title
        self.title.text=data[0]
        self.subtitle=self.slide.placeholders[1]
        self.subtitle.text=data[1]
        if data[2] != "":
        	self.slide.placeholders[2].insert_picture(data[2])

slides = [
    ["USA Weather",       #data[0]
     "Subtitle(Bullet)",
     "girl.png",
     8],
    ["Malaysia Weather",       #data[0]
     "Content(Bullet)",
     "",
     3],
    ["China Weather",       #data[0]
     "This is a brown Fox",
     "",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

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

Output

In case you got an error, try this code

from pptx import Presentation
import os

prs = Presentation()

class MySlide:
    def __init__(self, data):
        self.layout = prs.slide_layouts[data[3]]
        self.slide=prs.slides.add_slide(self.layout)
        self.title=self.slide.shapes.title
        self.title.text=data[0]
        self.subtitle=self.slide.placeholders[1]
        self.subtitle.text=data[1]

        for shape in self.slide.placeholders:
            print('%d %s %s' % (
             shape.placeholder_format.idx,
             shape.placeholder_format.type,
             shape.name))
            print()
        if data[2] != "":
        	self.img = self.slide.placeholders[1].insert_picture(data[2])

slides = [
    ["USA Weather",       #data[0]
     "Subtitle(Bullet)",
     "girls.png",
     8],
    ["Malaysia Weather",       #data[0]
     "Content(Bullet)",
     "",
     3],
    ["China Weather",       #data[0]
     "This is a brown Fox",
     "",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

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

This works for python 3.10 and above

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

prs = Presentation()

class MySlide:
    def __init__(self, data):
        self.layout = prs.slide_layouts[data[3]]
        self.slide=prs.slides.add_slide(self.layout)
        self.title=self.slide.shapes.title
        self.title.text=data[0]
        self.subtitle=self.slide.placeholders[1]
        self.subtitle.text=data[1]

        for shape in self.slide.placeholders:
            print('%d %s %s' % (
             shape.placeholder_format.idx,
             shape.placeholder_format.type,
             shape.name))
            print()
        if data[2] != "":
            self.img = self.slide.placeholders[1].insert_picture(data[2])

slides = [
    ["USA Weather",       #data[0]
     "Subtitle(Bullet)",
     "woman.png",
     8],
    ["Malaysia Weather",       #data[0]
     "Content(Bullet)",
     "",
     3],
    ["China Weather",       #data[0]
     "This is a brown Fox",
     "",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

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

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.