user1039698
user1039698

Reputation: 173

How to create a text shape with python pptx?

I want to add a text box to a presentation with python pptx. I would like to add a text box with several paragraphs in the specific place and then format it (fonts, color, etc.). But since text shape object always comes with the one paragraph in the beginning, I cannot edit first of my paragraphs. The code sample looks like this:

txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame

p = tf.add_paragraph()
p.text = "This is a first paragraph"
p.font.size = Pt(11)

p = tf.add_paragraph()
p.text = "This is a second paragraph"
p.font.size = Pt(11) 

Which creates output like this: enter image description here

I can add text to this first line with tf.text = "This is text inside a textbox", but it won't be editable in terms of fonts or colors. So is there any way how I can omit or edit that line, so all paragraphs in the box would be the same?

Upvotes: 10

Views: 14405

Answers (1)

scanny
scanny

Reputation: 29031

Access the first paragraph differently, using:

p = tf.paragraphs[0]

Then you can add runs, set fonts and all the rest of it just like with a paragraph you get back from tf.add_paragraph().

Upvotes: 12

Related Questions