Python 循环功能参数以创建电源点显示
我试图通过Python 循环功能参数以创建电源点显示,python,syntax,parameter-passing,user-defined-functions,python-pptx,Python,Syntax,Parameter Passing,User Defined Functions,Python Pptx,我试图通过pythonpptx 我有自己的power point模板,每张幻灯片都需要嵌入多张图像。我使用*允许函数具有任意数量的参数 例如,我有3个图像(.png)。我想把每张图片放在不同的幻灯片上,这里说3。 我尝试的代码: from pptx import Presentation from pptx.util import Inches def Create_PPT(oldFileName, newFileName, *img): prs = Presentation(oldF
pythonpptx
我有自己的power point模板,每张幻灯片都需要嵌入多张图像。我使用
*
允许函数具有任意数量的参数
例如,我有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:
...
太棒了
title.text=image
将为图像的名称创建一个标题。但是,它也包括.png
。可以删除标题中的.png
吗?我得到了答案,图像[:-4]