Python - 在已经存在的演示文稿中编辑 Powerpoint 幻灯片内容

标签 python python-2.7 ms-office powerpoint-2010

我在 Windows 7 上使用 Python 2.7.2 和 Office 2010。我有一个 Powerpoint 文件,其中包含指向目录中其他文档的链接。我想使用 Python 遍历形状标题,在目录中查找匹配的名称,并在 Powerpoint 文件中创建超链接。只要我能读懂那些形状的说明文字,我就可以完成剩下的事情。

我设法做到了这一点,并且至少打开了 Powerpoint 文件:

import win32com.client

Presentation = Application.Presentations.Open("c:\\path\\to\\stnd4.pptx")

我找到了许多方法来添加尚不存在的幻灯片以及添加形状或标题,但我似乎找不到任何方法来编辑现有 幻灯片。这让我可以添加一张幻灯片:

Base = Presentation.Slides.Add(1, 12)

但是任何试图打开或编辑现有幻灯片的操作都会失败:

Base = Presentation.Slides.Open(1)
Base = Presentation.Slides.Edit(1)

我也试过

help(Presentation)

但我只是得到一般的 win32com 信息,Powerpoint 幻灯片上什么也没有。谷歌搜索也没有出现太多。有什么线索吗?

最佳答案

在 PowerPoint 对象模型中没有“打开”或“编辑”幻灯片的概念。相反,您获得了对幻灯片的引用(我怀疑您的“Base = Presentation.Slides.Add(1, 12)”行完成了)。

PowerPoint 具有分层对象模型:演示文稿包含幻灯片,幻灯片包含形状,形状具有各种属性,您可以通过它们的属性和方法进行修改。

我不会用Python,也不知道它能和PPT沟通到什么程度,但是:

您的 BASE 对象(一张幻灯片,记住)可能有一个 Shapes 集合。遍历 Shapes 集合并为每个形状尝试如下操作:

If the shape's .HasTextFrame property is true then
   If the shape's .TextFrame.HasText property is true then
      The shape's .TextFrame.TextRange.Text property will return the text in the shape.

关于Python - 在已经存在的演示文稿中编辑 Powerpoint 幻灯片内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153541/

相关文章:

python - 如何正确卸载Anaconda?

python - somefile.close 和 somefile.close() 之间的区别

python - 合并数据框行以填充缺失的数据

python - html 绕过 noscript 标签

python - 在 wxPython 中使用线程将面板添加到 GUI

python - 在 python 中重命名 elif

c# - OpenXml 多图像

c# - 保存时执行的 Office 加载项

mysql - MS Access Access SQL Server?

python - 如何计算最近的半正定矩阵?