【发布时间】:2021-05-31 17:05:01
【问题描述】:
我目前正在使用python-pptx 制作一个 PowerPoint 模板。我在 MS Office 上创建了一个模板,并希望用文本和图像动态填充模板。因此,我创建了包含占位符的布局和幻灯片。
我在访问和填充文本占位符时没有问题,但是,我在使用图片占位符时遇到了问题。
我想使用PicturePlaceholder object 的.insert_picture() 方法。您还可以在文档here 中找到此方法的示例。
为此,我按如下方式访问占位符:
prs = Presentation(pathToPptxTemplate)
slide = prs.slides[indexOfMySlide]
# There are multiple image placeholders on my slide
image_placeholders = [
i for i in slide.placeholders if
i.placeholder_format.type == 18
]
for index, shape in enumerate(image_placeholders):
shape.insert_picture(pathToMyImage)
这会导致以下错误消息:
AttributeError: 'PlaceholderPicture' object has no attribute 'insert_picture'
在摸索了几分钟后,我注意到我正在查看PicturePlaceholder 的文档,但访问的是PlaceholderPicture 对象。我在文档中读到的不同之处在于:
- PicturePlaceholder 是只能接受图片的占位符形状。
而
- PlaceholderPicture 是填充有图片的占位符形状。
现在我还没有用任何图片填充占位符。我刚刚在 PowerPoint 中创建了一个空占位符。
这是我的问题:
如何在 PowerPoint 中创建 PicturePlacholder 以及如何使用 Python-pptx 访问它?
如果这不可能,您有替代解决方案吗?
提前致谢
【问题讨论】:
标签: python python-pptx