使用 python-pptx 将文本/幻灯片标题添加到幻灯片上的占位符
我正在尝试为幻灯片添加标题.我正在查找文档 here 它说, "设置幻灯片标题几乎所有的幻灯片布局都有一个标题占位符,任何基于布局的幻灯片在应用布局时都会继承它.访问幻灯片的标题是一个常见的操作,形状树上有一个专门的属性:
I am trying to add a title to my slide . I was looking up the documentation here and it says, "Setting the slide title Almost all slide layouts have a title placeholder, which any slide based on the layout inherits when the layout is applied. Accessing a slide’s title is a common operation and there’s a dedicated attribute on the shape tree for it:
title_placeholder = slide.shapes.title
title_placeholder.text = 'Air-speed Velocity of Unladen Swallows'
"
实际上我的幻灯片布局没有 title_placeholder.
Actually my slide layout does not have a title_placeholder.
我运行此代码以找出我拥有哪些占位符:
I ran this code to find out which placeholders I have :
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
所以现在我想向这些占位符添加文本 - 其中之一是标题 - 我会弄清楚是哪一个.
So Now I'd like to add text to these placeholders - one of them is the Title - I'll figure out which one.
文档有如何使用insert_picture
、insert_table
等在占位符中插入表格、图片和图表.但没有insert_text
.
The documentation has how to insert table, picture and chart in a placeholder using insert_picture
, insert_table
etc. But no insert_text
.
如何添加文字?
在 python-pptx
中,占位符是 Shape
(自动形状)的子类.因此,在占位符上可以使用常规形状(如矩形)的所有属性,特别是 .text
和 .text_frame
.
In python-pptx
, a placeholder is a subclass of Shape
(auto shape). So all the properties of a regular shape (like a rectangle) are available on a placeholder, in particular .text
and .text_frame
.
http://python-pptx.readthedocs.io/en/latest/api/shapes.html#shape-objects-autoshapes
因此要更改占位符的文本,只需使用:
So to change the text of a placeholder, simply use:
placeholder.text = 'foobar'
执行此操作的常用方法是使用预先格式化为标准标题外观的标题占位符.通常,标题占位符放置在用于创建幻灯片的幻灯片版式上,这样它在演示文稿中始终如一地显示.
The usual way to do this is to have a title placeholder that is pre-formatted for the standard title look and feel. Generally the title placeholder is placed on the slide layout used to create the slide, that way it shows up consistently through the presentation.
使用标题占位符可确保它包含的文本用于在大纲视图和其他一些位置识别幻灯片.
Using the title placeholder ensures the text it contains is used to identify the slide in the outline view and a few other places.