scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 520 forks source link

python-pptx fit text within text placeholder #715

Open soumyand opened 3 years ago

soumyand commented 3 years ago

python-pptx while inserting text in predefined pptx template text placeholder i am using below options to fit text in text placeholder. TP.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE TP.word_wrap = True TP.fit_text()

none of options fit text properly inside placeholder. text is overflowing out of placeholder. when opened ppt manually we have to set autofit text.

How to set long text in a small text placeholder using python-pptx

Delengowski commented 3 years ago

So out of curiosity what version of PowerPoint are you using?

Assuming the settings you gave above,

For >= PowerPoint 2016 when you open the font will not be autosized. However, simply saving the pptx file will cause the GUI to do the autosizing.

For <= PowerPoint 2013, this autosizing never happesn, not even with a save. You can go into the settings of the placeholder and you'll see the correct settings but it wont trigger. What you need to do is de-select text-to-fit-shape and then re-select it, this will cause the sizing to occur.

astrophysaxist commented 3 years ago

I'm seeing a similar issue. Except after opening the created pptx file and saving the pptx, text remains overflowing. I'm using the latest powerpoint (Version 2107) which is through whatever enterprise account we have set up. I wonder if this a problem because this is a Microsoft 365 App.

astrophysaxist commented 3 years ago

Dropping a line to say I was able to get the autosizing to work in my pptx file by following similar instructions to the <=PowerPoint 2013 @Melendowski mentioned above. I opened the Slide Master, navigated to the slide layout in question; and on the placeholder shape's format menu Text Options tab, clicked 'Do not Autofit', then reselected 'Shrink text on overflow'. I exited the Slide Master and lo' and behold the text now fits in the placeholder box.

MartinPacker commented 3 years ago

That last might be automatable. Whether with AppleScript or VBA.

scanny commented 3 years ago

In my experience all it takes to trigger "re-flow" is to place the insertion-point in the text (click on text with mouse). At worst you need to add a space and then backspace over it. This is still a pain, but perhaps less likely to change some setting you didn't intend to. Interestingly, I've heard folks say this isn't a problem in LibreOffice, which automatically re-flows on load.

MartinPacker commented 3 years ago

I wonder if there's a VBA solution. But how to inject suitable VBA would be an interesting question. (As a Mac user I also wonder about AppleScript / JXA for this task.)

ARTony8-etu commented 1 year ago

Hello, thanks for the work for this package. @scanny

I'm really interested if someone found a solution to this problem meanwhile.

I'm using powerpoint 2016 and the text doesn't autofit. (textframe.autosize = MSO.FIT_TEXT_TO_SHAPE). I need to go open the presentation and add a whitespace and remove it so that it trigger the autofit like you mentionned all.

Anyone have a solution

MichaelPanwenjie commented 5 months ago

i am still face the issue, anyone found the solution to set autofit automaticlly?