7

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?

Akshat Zala
  • 710
  • 1
  • 8
  • 23
user1039698
  • 143
  • 1
  • 6

1 Answers1

9

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().

scanny
  • 26,423
  • 5
  • 54
  • 80