简体   繁体   English

通过使用pptx生成的ppt在PowerPoint和libreoffice中的不同行为

[英]Different behavior in powerpoint and libreoffice by ppt generated using python-pptx

I am trying to create a file using python-pptx on a flask server. 我正在尝试在Flask服务器上使用python-pptx创建文件。 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. 当我尝试在我的代码中使用text_frame.auto_size时,所有这些都可以正常工作,甚至下载也可以正常工作。 LibreOffice Impress displays the text perfectly but MS Powerpoint does not display the text properly. LibreOffice Impress可以完美显示文本,但是MS Powerpoint不能正确显示文本。

Here are the images explaining the issue - 以下是说明问题的图片-

LibreOffice - LibreOffice- 在此处输入图片说明

Powerpoint - 微软幻灯片软件 - 在此处输入图片说明

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? 知道我在这里做错了吗?

This is unfortunately a limitation of PowerPoint and an unusual (in my experience) place where LibreOffice actually does better. 不幸的是,这是PowerPoint的局限性,而且(以我的经验)这是LibreOffice实际做得更好的地方。

You'll notice that if you click in the PowerPoint text, insert a space and then delete it, that the text will automatically autofit. 您会注意到,如果您单击PowerPoint文本,插入一个空格然后将其删除,该文本将自动自动调整。 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 . 在幻灯片后面的XML中,将缓存文本框的当前“自动调整”字体大小。 LibreOffice automatically recalculates this cached figure when the presentation opens; 演示文稿打开时,LibreOffice会自动重新计算该缓存的图形; PowerPoint does not. PowerPoint没有。

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). 计算“自动调整”的字体大小是渲染引擎的工作,它可以访问字体大小,换行/分行符等python-pptx不包括渲染引擎,也不能访问一个(不存在)据我所知,适用于Python)。 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. 但是, .fit_text()方法的形式有一项实验性功能,可以帮助您实现大部分目标。 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. 如果可以做得更好,您可以自由进行阐述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python-pptx增加python生成的powerpoint演示文稿的幻灯片大小 - how to increase the slide size a python generated powerpoint presentation using python-pptx Django使用python-pptx库生成的powerpoint有错误信息 - Django the powerpoint generated using python-pptx library has error message 在幻灯片布局 3 中使用 python-pptx 在 ppt 中添加数据框和文本 - Adding dataframe and text in ppt using python-pptx in slide layout 3 使用 python-pptx 替换 ppt 各方面的特定文本 - Replacing particular text in all sides of a ppt using python-pptx 如何使用 pandas 和 python-pptx 修改 powerpoint 中的现有表? - How to modify existing tables in powerpoint using pandas and python-pptx? 使用 python-pptx 自动重新排列 powerpoint 幻灯片 - Rearrange powerpoint slides automatically using python-pptx python-pptx“ .ppt”处理工作 - python-pptx “.ppt” handling work around 使用python-pptx检查powerpoint中的图像是否具有装饰性 - Check if image is decorative in powerpoint using python-pptx Python-pptx 与 LibreOffice 中的文本框的兼容性 - Python-pptx compatibility with text boxes in LibreOffice 使用python-pptx创建PPT时如何使用我公司的PPT幻灯片布局? - How to use my company's PPT slide layouts while creating a PPT using python-pptx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM