问题
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'
"
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))
and got:
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.
The documentation has how to insert table, picture and chart in a placeholder using insert_picture
, insert_table
etc. But no insert_text
.
How do I add text?
回答1:
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.
来源:https://stackoverflow.com/questions/40853330/adding-text-slide-title-to-placeholder-on-slide-with-python-pptx