scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.38k stars 515 forks source link

Identify size of the default text_frame on a shape #663

Open darsh10 opened 3 years ago

darsh10 commented 3 years ago

Hi python-pptx,

I have created a rectangle shape in my powerpoint with a fixed size. There is text written inside the shape. However, the text is long and it overflows. This is expected. When I open the slide in keynote or google slides, the text has a larger text box. I would like to understand how I can programmatically identify the locations where the text overflows.

Thank you, Darsh.

scanny commented 3 years ago

You can't, at least not very reliably and not easily. What you can do is set the shape autofit setting to not resize to fit text and not to wrap. Then the shape should stay its original size. The reason you see different behavior is that Keynote and Google Slides refresh the shape wrapping/autosize on load whereas PowerPoint does not (until you adjust the shape in the smallest way).

The properties that control these behaviors are on the TextFrame object accessed via shape.text_frame and documented here: https://python-pptx.readthedocs.io/en/latest/api/text.html#text-related-objects.

darsh10 commented 3 years ago

Hi Scanny,

Thanks a lot for the pointer. I have gone through this documentation. I was curious to learn if there is a way for us to identify the overflow. I do see the text in the original size. Just want to detect the cases when there is an overflow automatically.

scanny commented 3 years ago

Ah, right, I see now better what you're after. Well, the problem is we have no access to the rendering engine and approximating its text layout behavior is beyond the capabilities of standard Python packages (like PIL) last time I looked, which was when the TextFrame.fit_text() method was written. Maybe things have come along since then. Probably a direct-access-to-PowerPoint-application API like Visual Basic (VBA) or the win32com interface (Windows only) would have access to the rendering engine somehow and be able to tell you at least something about when the text was too wide. Maybe Qt or some package like that has better font-metrics discovery and could do better than we were able to with PIL a few years back.

MartinPacker commented 3 years ago

It has occurred to me that one could write VBA or (in my case) AppleScript to post-process the .pptx made by python-pptx.

(In my md2pptx project - based on python-pptx - I'm thinking of allowing a post-processing exit point. I've not found a good case for doing so - and I'm not sure your need is it.)