遍历函数参数以创建 powerpoint 演示文稿
loop through function parameters to create power point presentaion
我正在尝试定义函数以通过 python-pptx
生成 power point 演示文稿。
我有自己的 PowerPoint 模板,每张幻灯片都需要嵌入多张图片。我使用 *
来允许任意数量的函数参数。
例如,我有 3 个图像 (.png
)。我想把每张图片放在不同的幻灯片里,这里说3张。
我试过的代码:
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')
我收到错误:
for image in *img:
^
SyntaxError: invalid syntax
此外,我认为我的代码不完整。要循环播放并添加幻灯片,我想我还需要添加更多语法。
for index, _ in enumerate(prs.slide_layouts):
slide = prs.slides.add_slide(prs.slide_layouts[index])
然而,这不是正确的。上面的代码只是循环创建不同的幻灯片布局。我的幻灯片布局是固定的,9
这里。
因此,我认为我需要的是遍历 prs.slides.add_slide()
,但不确定这一点,因为我每次尝试都会出错。
输出将是 3 张幻灯片,每张幻灯片都有图片,每张幻灯片的标题是图片名称,test1
、test2
和 test3
。
对此有何建议?
img
是一个列表,我想你只是想遍历它:
for image in img:
...
我正在尝试定义函数以通过 python-pptx
生成 power point 演示文稿。
我有自己的 PowerPoint 模板,每张幻灯片都需要嵌入多张图片。我使用 *
来允许任意数量的函数参数。
例如,我有 3 个图像 (.png
)。我想把每张图片放在不同的幻灯片里,这里说3张。
我试过的代码:
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')
我收到错误:
for image in *img:
^
SyntaxError: invalid syntax
此外,我认为我的代码不完整。要循环播放并添加幻灯片,我想我还需要添加更多语法。
for index, _ in enumerate(prs.slide_layouts):
slide = prs.slides.add_slide(prs.slide_layouts[index])
然而,这不是正确的。上面的代码只是循环创建不同的幻灯片布局。我的幻灯片布局是固定的,9
这里。
因此,我认为我需要的是遍历 prs.slides.add_slide()
,但不确定这一点,因为我每次尝试都会出错。
输出将是 3 张幻灯片,每张幻灯片都有图片,每张幻灯片的标题是图片名称,test1
、test2
和 test3
。
对此有何建议?
img
是一个列表,我想你只是想遍历它:
for image in img:
...