Python pptx - stop reading slide based on conditions

Viewed 48

I am hoping to read the content of a bunch of slides with same features. The information I need is either on the first 2 slides or first 3 slides. But I don't know which ones have 2 useful slides and which ones have 3, what would be a good stopping condition for my case?

I was thinking to use some features of the page immediately following it to break out of the loop, but I don't know how pptx reads the file -- does it read text boxes first? Does it read tables first? Does it read from top down left to right for tables etc.?

Any thought or suggestions?

1 Answers

A slide contains shapes, some types of which can contain text. The shapes on a slide are in slide.shapes and appear in z-order, meaning the bottom-most shape appears first and each subsequent shape is "layered on top" of that shape. If the shapes don't overlap then it probably doesn't matter much, but when the do overlap, part or all of the shape below may be obscured by the shape above.

You can distinguish shapes that can contain text from those that cannot using the shape.has_text_frame property.

I'd say read your content from the shapes and stop once you've gotten everything, like:

for shape_idx, shape in enumerate(slide.shapes):
    if not shape.has_text_frame:
        continue
    # --- ... inspect shape content ... ---
    if all_content_is_collected() or shape_idx > 1:
        break

You're the one that will have to determine whether all the content you're after is collected (by implementing all_content_is_collected()). The loop will break after processing slide.shapes[2], which is the third slide.

Related