python pptx unable to auto fit the text box/frame window boundary

32 views Asked by At

I have this PPT slide which has few text boxes. Now, through my code, I am inserting content into each of these text boxes and what I want is that the text box boundary/outline should auto fit and expand to show the entire text of that text box within the boundary. Here's my code so far:

def insert_text(text_frame, content_texts):
    
    # Add content texts to the text frame
    for content_text in content_texts:
        paragraph = text_frame.add_paragraph()
        paragraph.text = content_text
        paragraph.space_after = Pt(12)  

    text_frame.fit_text(font_family='Arial', max_size=12, bold=False, italic=False)
    #this one throws error as not a valid method for text_frame
    # text_frame.autofit_text()
    text_frame.word_wrap = False # I tried setting it to true as well
    text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
    

def new_slide(slide_path, texts):
    
    ppt_template = Presentation(slide_path)
    #this is a sample code line
    slide = ppt_template.slides[0]

    for section_text, content_texts_array in texts.items():
        if slide is not None:
            #this is a sample code line to get text_frame
            for shape in slide.shapes:
                text_frame = shape.text_frame
            if text_frame is not None:
                # Add content texts to the same text frame and adjust text box size
                insert_text(text_frame, content_texts_array)

When I execute above code, I can see the text being added to the text boxes, but the window/outline of the text box doesn't resize to cover the entire text. Output looks something like this:

enter image description here

As you can see, the text box boundary doesn't auto adjust and remains same as it was in the PPT before the content were inserted into the text box/frame. The third sentence in the image is still inside the text box, but the bondary didn't adjusted to show it so.

How do I auto-resize the text boxes so that they cover the entire content within their boundary and also don't overlap with other text boxes either above or below them?

0

There are 0 answers