使用 python-pptx 将文本/幻灯片标题添加到幻灯片上的占位符

Adding text / slide title to placeholder on slide with python-pptx

我正在尝试为我的幻灯片添加标题。我正在查找文档 here,它说,“设置幻灯片标题 几乎所有幻灯片布局都有一个标题占位符,任何基于该布局的幻灯片在应用该布局时都会继承该占位符。访问幻灯片的标题是一个常见的操作,形状树上有一个专门的属性:

title_placeholder = slide.shapes.title
title_placeholder.text = 'Air-speed Velocity of Unladen Swallows'

实际上我的幻灯片布局没有 title_placeholder。

我运行此代码找出我有哪些占位符:

for shape in slide.placeholders:
...     print('%d %s' % (shape.placeholder_format.idx, shape.name))

并得到:

11 Text Placeholder 3
12 Text Placeholder 4
13 Text Placeholder 2
15 Text Placeholder 1

所以现在我想向这些占位符添加文本 - 其中之一是标题 - 我会找出哪个。

文档有如何使用 insert_pictureinsert_table 等在占位符中插入 table、图片和图表。但没有 insert_text

如何添加文字?

python-pptx中,占位符是Shape(自动形状)的子类。因此,规则形状(如矩形)的所有属性都可以在占位符上使用,特别是 .text.text_frame.

http://python-pptx.readthedocs.io/en/latest/api/shapes.html#shape-objects-autoshapes

因此,要更改占位符的文本,只需使用:

placeholder.text = 'foobar'

通常的做法是使用 pre-formatted 的标题占位符来实现标准标题的外观。通常,标题占位符放置在用于创建幻灯片的幻灯片布局上,这样它在整个演示文稿中始终如一地显示。

使用标题占位符可确保它包含的文本用于在大纲视图和其他一些地方标识幻灯片。