2

I am creating a textbox on a PowerPoint slide with the python-pptx package. I am using what I think is the correct auto-size attribute at the end of the below code but the textbox does not expand to fit the text. Any suggestions on what I am doing wrong? Thanks!

from pptx.util import Cm, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_AUTO_SIZE

layout = ppt.slide_layouts[5]
slide = ppt.slides.add_slide(layout)

textbox = slide.shapes.add_textbox(left=Cm(1), top=Cm(5), width=Cm(8), height=Cm(0.8))
textbox.fill.solid() # Set fill type to solid colour first
textbox.fill.fore_color.rgb = RGBColor(223,137,255) # Set textbox colour

textbox1.text_frame.paragraphs[0].text = "Really long label that the textbox shape should expand to fit"
textbox1.text_frame.paragraphs[0].font.name = "Arial"
textbox1.text_frame.paragraphs[0].font.size = Pt(10)
textbox1.text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)

textbox1.text_frame.word_wrap = True
textbox1.text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT # This is the part that should tell the textbox to expand to fit the text but doesn't seem to have any effect
jim_jones
  • 167
  • 3
  • 13

0 Answers0