python pptx: How to check if text in two placeholders overlap

34 views Asked by At

I'm using python-pptx to create a presentation.

I have a slide with 2 Text placeholders, the 1st above the 2nd (the same x-coord (shape.left)). When I put the text in both, the text in the 1st shape is long and overlap the text in the shape below. If I find out that there is the overlap, I can move the 1st shape (text) up or move the 2nd text down. But how to check the overlap?

Or how to get height of text in placeholder/text box? (then I can calculate vertical size and find overlap).

The python is preferred language but the package can be any, so you can suggest any package if it can solve it.

I can provide some code if needed.

EDIT: Yes, I'm looking for a solution in python (regardless package). Code example:

import time
from pptx import Presentation as pptx_Presentation
from pptx.enum.text import MSO_AUTO_SIZE

def get_date_time_str():
    """Return string of "now" in %Y%m%d-%H%M%S format"""
    return time.strftime("%Y%m%d-%H%M%S", time.localtime())

long_text = "F" * 60
empty = r"C:\f\empty.pptx"  # PowerPoint file where is "Sections" layout and no slide
new = pptx_Presentation(empty)

slide_layout = new.slide_layouts[1]
# there are 2 Text Placeholders, both have the same shape.left so they are one above another

# Example 1: Set text directly into text_frame
slide1 = new.slides.add_slide(slide_layout)
for shape in slide1.shapes:
    shape.text_frame.text = long_text

# Example 2: Set text in paragraphs
# 3 paragraphs for each Text Placeholder
# NOTE: sometimes I also set text in runs in paragraphs
slide2 = new.slides.add_slide(slide_layout)
pars_data = [long_text for i in [1,2,3]]
wrap = True # False

for shape in slide2.shapes:
    text_frame = shape.text_frame
    text_frame.word_wrap = wrap
    # also tested setting 'auto_size' before putting text
    # text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
    text_frame.paragraphs[0].text = pars_data[0]

    for par_data in pars_data[1:]:
        par = text_frame.add_paragraph()
        par.text = par_data

    text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT

date_str = get_date_time_str()
out = rf"C:\f\out-{date_str}.pptx"
new.save(out)
0

There are 0 answers