Fit shape to text with Python pptx

Viewed 18

I am creating a textbox on a PowerPoint slide with the python-pptx package. I want to iteratively add text to this text box while expanding automatically the shape size according to the text it has. So far I followed the documentation of the library here But when I open the presentation I noticed that the size of the shape didn't change however it adapts to the text when I type something inside the shape on the PowerPoint itself. an Example code of what I used is below. Does anyone have a solution for this or it's just a bug in the library? Thanks. code

from pptx import Presentation
from pptx.enum.text import  MSO_AUTO_SIZE
from pptx.enum.text import PP_ALIGN


prs = Presentation(r"C:\...\template.pptx")
title_slide_layout = prs.slide_layouts[4]
slide = prs.slides.add_slide(title_slide_layout)
text_shape = slide.shapes.placeholders[10]
tf = text_shape.text_frame
tf.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
for i in range(500):
    p = tf.add_paragraph()
    r=p.add_run()
    r.text = "test"
    print(text_shape.height)
tf.word_wrap = False
prs.save(r"C:\....\test.pptx")
0 Answers
Related