0

I am trying to create a file using python-pptx on a flask server. All of this is working and even the download is working by the problem comes when I try to use text_frame.auto_size in my code. LibreOffice Impress displays the text perfectly but MS Powerpoint does not display the text properly.

Here are the images explaining the issue -

LibreOffice - enter image description here

Powerpoint - enter image description here

Also, here is the code that I am using -

text_box = slide.shapes.add_textbox(left, top, width, height)
text_frame = text_box.text_frame
text_frame.word_wrap = True
text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE

Any idea what I am doing wrong here?

yashdosi
  • 1,186
  • 1
  • 18
  • 40

1 Answers1

1

This is unfortunately a limitation of PowerPoint and an unusual (in my experience) place where LibreOffice actually does better.

You'll notice that if you click in the PowerPoint text, insert a space and then delete it, that the text will automatically autofit. This may not fix the problem, but it points to the cause.

In the XML behind the slide, the current "auto-fitted" font size of the textbox is cached. LibreOffice automatically recalculates this cached figure when the presentation opens; PowerPoint does not.

Calculating the "auto-fitted" font size is the job of the rendering engine, which has access to font sizes, line/word breaks, etc. python-pptx does not include a rendering engine nor does it have access to one (none exists for Python as far as I know). So the best it can do is estimate it and it prefers not to do that, since that's getting into rendering.

However, there is an experimental feature in the form of the .fit_text() method that may get you most of the way there. Basically, that capability was so wanted that someone was willing to sponsor a "best-efforts" solution, which is what that method represents. The documentation at that link explains how to use it and its limitations.

Note that that method is experimental, meaning it won't be considered a bug if it doesn't work the way you need it to. You're free to elaborate it if you can do better.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Thanks for the reply! But I am running my code on a CentOS machine. And fit_text tried to get font info which throws "Unsupported Operating System" error for me. I also read somewhere that there is a pull request waiting to be merged which will fix this issue. But until then I cannot use fit_text. :/ – yashdosi Jan 28 '17 at 07:21