无法使用 Python 在图片占位符 powerpoint 中添加图像

我想使用图片占位符自动替换 powerpoint 中的图像

我使用 python 使其动态化,但我遇到了这样的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'LayoutPlaceholder' object has no attribute 'insert_picture'

我使用的代码如下:

    from pptx import Presentation 
    from pptx.util import Inches    from PIL import Image
    prs = Presentation(r'..\gambar\base_master_mod.pptx')
    slide = prs.slide_layouts[0]
    placeholder = slide.placeholders[1] # id key, not posisition 
    placeholder.name
    placeholder.placeholder_format.type
    picture = placeholder.insert_picture("..\output_graphics\image_res.png")

这里是我使用的 powerpoint:这里

如何解决问题?


慕桂英546537
浏览 235回答 1
1回答

慕莱坞森

将第 5 行更改为:slide&nbsp;=&nbsp;prs.slides[0]看看这是否能让你更接近。您可能仍然需要使用占位符 id 来弄清楚它们是什么。你可以做这样的事情来列出它们。for&nbsp;ph&nbsp;in&nbsp;slide.placeholders: &nbsp;&nbsp;&nbsp;&nbsp;print("placeholder&nbsp;%d&nbsp;has&nbsp;name:&nbsp;%s"&nbsp;%&nbsp;(ph.placeholder_format.idx,&nbsp;ph.name)布局占位符与具有不同行为的幻灯片占位符不同。该.insert_picture()方法仅适用于幻灯片图片占位符。这就是它说的原因LayoutPlaceholder has no attribute 'insert_picture'。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python