I am trying to define function to generate a power point presentation by python-pptx
.
I have my own power point template and multiple images needed to be embedded into each slide. I used *
to allow arbitrary number of arguments to functions.
For example, I have 3 iamges (.png
). I want to put each image in different slides, said 3 here.
The code I tried:
from pptx import Presentation
from pptx.util import Inches
def Create_PPT(oldFileName, newFileName, *img):
prs = Presentation(oldFileName)
# Create the slides for images
for image in *img:
graph_slide_layout = prs.slide_layouts[9] # 9 is the customized template I create in my oldfile.
slide = prs.slides.add_slide(graph_slide_layout)
title = slide.shapes.title
title.text = image
left = Inches(0.7)
top = Inches(0.75)
height = Inches(6)
width = Inches(12)
pic = slide.shapes.add_picture(image, left, top, width = width, height = height)
prs.save(newFileName)
Create_PPT('mystyle.pptx', 'new.pptx', 'test1.png', 'test2.png', 'test3.png')
I got the error:
for image in *img:
^
SyntaxError: invalid syntax
Moreover, I think my code is incomplete. To loop through and add the slides, I guess I also need to add more syntax.
for index, _ in enumerate(prs.slide_layouts):
slide = prs.slides.add_slide(prs.slide_layouts[index])
However, this is not correct one. Above code just loop through to create different slide layouts. My slide layout is fixed, 9
here.
Therefore, I think what I need is to loop through prs.slides.add_slide()
, but not sure about this since I got errors each try.
The output will be 3 slides with image on each and the title of each slide is images' name, test1
, test2
, and test3
.
Any suggestion on this?