简体   繁体   English

在 PowerPoint 中创建“PicturePlaceholder”并使用 Python-pptx 访问它

[英]Creating a `PicturePlaceholder` in PowerPoint and accessing it with Python-pptx

I'm currently working on a PowerPoint template with python-pptx .我目前正在使用python-pptx制作 PowerPoint 模板。 I have created a template on MS Office and would like to dynamically populate the template with both text and images.我在 MS Office 上创建了一个模板,并希望用文本和图像动态填充模板。 Therefore, I have created layouts and slides containing placeholders.因此,我创建了包含占位符的布局和幻灯片。

I have no problems accessing and populating the text placeholders, however, I'm struggling with the picture placeholders.我在访问和填充文本占位符时没有问题,但是,我正在为图片占位符而苦苦挣扎。

I would like to use the .insert_picture() method of the PicturePlaceholder object .我想使用PicturePlaceholder object.insert_picture()方法。 You can also find an example of this method in the documentation here .您还可以在此处的文档中找到此方法的示例。

To do so I am accessing my placeholders as follows:为此,我正在访问我的占位符,如下所示:

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)

This results in the following error message:这会导致以下错误消息:

AttributeError: 'PlaceholderPicture' object has no attribute 'insert_picture'

After scratching my head for a few minutes, I noticed that I was looking at the documentation for PicturePlaceholder but accessing a PlaceholderPicture object.挠了几分钟后,我注意到我正在查看PicturePlaceholder的文档,但访问的是PlaceholderPicture object。 The difference I read in the documentation is that:我在文档中读到的区别是:

  • a PicturePlaceholder is a Placeholder shape that can only accept a picture. PicturePlaceholder 是只能接受图片的占位符形状。

whereas然而

  • a PlaceholderPicture is a Placeholder shape populated with a picture. PlaceholderPicture 是填充有图片的占位符形状。

Now I have not populated the Placeholder with any picture yet.现在我还没有用任何图片填充占位符。 I have just created an empty placeholder in PowerPoint.我刚刚在 PowerPoint 中创建了一个空占位符。

So here is my question:所以这是我的问题:
How can I create a PicturePlacholder in PowerPoint and how can I access it with Python-pptx?如何在 PowerPoint 中创建 PicturePlacholder 以及如何使用 Python-pptx 访问它?

If this is not possible, do you have an alternative solution?如果这不可能,您有替代解决方案吗?

Thanks in advance提前致谢

Try deleting the offending "placeholder" shape and inserting a new, untouched placeholder using the PowerPoint UI.尝试使用 PowerPoint UI 删除有问题的“占位符”形状并插入一个新的、未触及的占位符。 On my Mac with PowerPoint 2016 this is:在我的带有 PowerPoint 2016 的 Mac 上,这是:

  • View > Slide Master视图 > 幻灯片母版
  • Select the desired slide-layout Select 所需的幻灯片布局
  • Slide Master > Insert Placeholder > Picture幻灯片母版 > 插入占位符 > 图片

You can adjust the size and position if you want, but otherwise leave it untouched and try again.您可以根据需要调整大小和 position,否则请保持不变并重试。 Probably best to do all this with a new presentation and delete all but one of the slide layouts and all the slides, just to keep things simple.最好用一个新的演示文稿来完成所有这一切,并删除除一个幻灯片布局和所有幻灯片之外的所有幻灯片,以保持简单。

Then use python-pptx to create a slide from that layout:然后使用python-pptx从该布局创建幻灯片:

slide = prs.slides.add_slide(prs.slide_layouts[0])

I think you're going to find that works and there is some way that your template placeholder has had an image inserted into it, perhaps to act as an example or something.我认为您会发现它有效,并且您的模板占位符通过某种方式将图像插入其中,也许可以作为示例或其他方式。

A couple possible areas of confusion:几个可能的混淆领域:

  1. A user does not create a placeholder shape on a slide.用户不会在幻灯片上创建占位符形状。 A slide inherits the placeholders of its slide layout when the slide is created.创建幻灯片时,幻灯片会继承其幻灯片版式的占位符。

  2. A PicturePlaceholder shape has the element <p:sp> , as do all placeholders. PicturePlaceholder形状具有元素<p:sp> ,所有占位符也是如此。 When a picture is "inserted into the placeholder" the p:sp element is replaced with a <p:pic> element;当图片“插入占位符”时, p:sp元素被替换为<p:pic>元素; the placeholder shape is replaced with a picture shape.占位符形状被替换为图片形状。

I think what you'll find is that the picture placeholder on your template slide has already been "used" and is now a picture.我想你会发现模板幻灯片上的图片占位符已经被“使用”过,现在是一张图片。 You can confirm this by inspecting the xml with print(shape.xml) .您可以通过使用print(shape.xml)检查 xml 来确认这一点。

The solution would be to fix that slide by recreating it, or it might work to open it in PowerPoint and delete its contents (not the whole shape) which should return it to its original picture placeholder state.解决方案是通过重新创建该幻灯片来修复它,或者它可以在 PowerPoint 中打开它并删除其内容(不是整个形状),这应该将其返回到其原始图片占位符 state。 It might also work to delete the offending shape and then "re-apply Layout" (possibly "Reset" in the slide layout area of the Home toolbar).它也可以删除有问题的形状,然后“重新应用布局”(可能在主页工具栏的幻灯片布局区域中“重置”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM