Python PPTX 幻灯片版式导入
Python PPTX Slide Layout Import
抱歉,我一直在寻找解决方案,但找不到足够的文档来解决这个问题。我正在尝试导入学校所需的默认幻灯片布局,它有一个特殊的背景和一个标题栏和一个字幕栏。我假设当我导入它时,python-pptx 会自动为这两个文本块创建占位符 0 和 1,但是当我尝试编辑占位符时,出现属性错误:
AttributeError: 'Presentation' object has no attribute 'placeholders'
我的代码如下:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
Presentation_Title = prs.placeholders[0]
Presentation_Subtitle = prs.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
编辑[0]:我知道我只是打开那个特定的演示文稿,但我如何访问和编辑其中的单张幻灯片?
编辑[1]:我发现一些 2015 年的帖子 python-pptx 扩展了此功能,但没有进一步的信息表明它确实发生了。
python-pptx 如何为导入的幻灯片布局分配占位符?或者它甚至会这样做吗?是否需要是 .potx 文件?
提前致谢。
占位符属于 幻灯片 object,而不是演示文稿 object。所以第一件事就是拿到一张幻灯片。
幻灯片是从幻灯片布局创建的,它本质上是克隆以获得一些起始形状,在许多情况下包括占位符。
所以第一步是弄清楚你想要哪种幻灯片版式。最简单的方法是打开 "starting" 演示文稿(有时称为 "template" 演示文稿)并使用“查看”>“母版”>“幻灯片母版...”菜单选项检查其幻灯片母版和布局。
找到您想要的那个,从第一个版式开始倒数,从 0 开始,这将为您提供该幻灯片版式的 index。
那么你的代码看起来像这样:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
slide_layout = prs.slide_layouts[0] # assuming you want the first one
slide = prs.slides.add_slide(slide_layout)
Presentation_Title = slide.placeholders[0]
Presentation_Subtitle = slide.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
就索引访问而言,placeholders
collection 的行为类似于 dict
,因此上面用作索引的 0 和 1 不太可能与您的情况完全匹配(尽管0 可能会起作用;标题始终为 0)。
本文档页面介绍了如何发现您的模板可用的索引:http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html
上一页有更多关于占位符概念的信息:
http://python-pptx.readthedocs.io/en/latest/user/placeholders-understanding.html
抱歉,我一直在寻找解决方案,但找不到足够的文档来解决这个问题。我正在尝试导入学校所需的默认幻灯片布局,它有一个特殊的背景和一个标题栏和一个字幕栏。我假设当我导入它时,python-pptx 会自动为这两个文本块创建占位符 0 和 1,但是当我尝试编辑占位符时,出现属性错误:
AttributeError: 'Presentation' object has no attribute 'placeholders'
我的代码如下:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
Presentation_Title = prs.placeholders[0]
Presentation_Subtitle = prs.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
编辑[0]:我知道我只是打开那个特定的演示文稿,但我如何访问和编辑其中的单张幻灯片?
编辑[1]:我发现一些 2015 年的帖子 python-pptx 扩展了此功能,但没有进一步的信息表明它确实发生了。
python-pptx 如何为导入的幻灯片布局分配占位符?或者它甚至会这样做吗?是否需要是 .potx 文件?
提前致谢。
占位符属于 幻灯片 object,而不是演示文稿 object。所以第一件事就是拿到一张幻灯片。
幻灯片是从幻灯片布局创建的,它本质上是克隆以获得一些起始形状,在许多情况下包括占位符。
所以第一步是弄清楚你想要哪种幻灯片版式。最简单的方法是打开 "starting" 演示文稿(有时称为 "template" 演示文稿)并使用“查看”>“母版”>“幻灯片母版...”菜单选项检查其幻灯片母版和布局。
找到您想要的那个,从第一个版式开始倒数,从 0 开始,这将为您提供该幻灯片版式的 index。
那么你的代码看起来像这样:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
slide_layout = prs.slide_layouts[0] # assuming you want the first one
slide = prs.slides.add_slide(slide_layout)
Presentation_Title = slide.placeholders[0]
Presentation_Subtitle = slide.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
就索引访问而言,placeholders
collection 的行为类似于 dict
,因此上面用作索引的 0 和 1 不太可能与您的情况完全匹配(尽管0 可能会起作用;标题始终为 0)。
本文档页面介绍了如何发现您的模板可用的索引:http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html
上一页有更多关于占位符概念的信息: http://python-pptx.readthedocs.io/en/latest/user/placeholders-understanding.html