I made some changes in the script to create a powerpoint slides with python (through python-pptx). I think that is more readable (IMO) and so, here it is, with an example that explain itself.
from pptx import Presentation
import os
prs = Presentation()
layout = prs.slide_layouts[1]
def create_slides(filename, content):
content = content.splitlines()
for s in content:
if "#" in s:
tit, cont = s.split("#")
slide = prs.slides.add_slide(layout)
slide.shapes.title.text = tit
slide.placeholders[1].text = cont
else:
pass
prs.save(filename)
os.startfile(filename)
content = """Python and Powerpoint # You can make great stuffs with Python to manipulate Powerpoint
Python pptx # You need to install python-pptx first
Create a new file # import Presentation from pptx and then create an istance of the class Presentation
Choose a layout # Powerpoint had different layouts for slides, use prs.layouts[1], it is the most generic
Create a new slide # Use the name of the istance (ex: prs) followed by .slides.add_slide(layout), where layout comes from layout = prs.slide_layouts[1], as I said in the previous slide
Title # add it with slide.shapes.title.text = "Some title"
subtitle # add it with slide.placeholders[1].text = "Some content"
"""
filename = create_slides("Example.pptx", content)
Live coding video to make slides in powerpoint with Python
The slides output:
Adding the same image on each page
In case you want to make a sort of template for each image, you can add this line to the code above:
slide.shapes.add_picture("python.png", 0, 0)
The whole code
from pptx import Presentation
import os
prs = Presentation()
layout = prs.slide_layouts[1]
def create_slides(filename, content):
content = content.splitlines()
for s in content:
if "#" in s:
tit, cont = s.split("#")
slide = prs.slides.add_slide(layout)
slide.shapes.title.text = tit
slide.placeholders[1].text = cont
slide.shapes.add_picture("python.png", 0, 0)
else:
pass
prs.save(filename)
os.startfile(filename)
content = """Python and Powerpoint # You can make great stuffs with Python to manipulate Powerpoint
Python pptx # You need to install python-pptx first
Create a new file # import Presentation from pptx and then create an istance of the class Presentation
Choose a layout # Powerpoint had different layouts for slides, use prs.layouts[1], it is the most generic
Create a new slide # Use the name of the istance (ex: prs) followed by .slides.add_slide(layout), where layout comes from layout = prs.slide_layouts[1], as I said in the previous slide
Title # add it with slide.shapes.title.text = "Some title"
subtitle # add it with slide.placeholders[1].text = "Some content"
"""
filename = create_slides("Example.pptx", content)
the output
How to insert in image in a placeholder
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",
3],
["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")








