简体   繁体   English

计算PowerPoint文本框的行数

[英]Count number of lines of a powerpoint textbox

I have been using python-pptx and if scanny sees this: Thank a lot for your work.我一直在使用 python-pptx,如果 scanny 看到这个:非常感谢您的工作。 It has been an absolute pleasure to work with this package, Every functionality I needed was some how available and if I couldn't find it.使用这款 package 非常高兴,我需要的每一个功能都是可用的,如果我找不到的话。 there was some answer on SO. SO上有一些答案。

Can I count the number of lines my textbox uses after I have inserted my text?插入文本后,我可以计算文本框使用的行数吗? According to my reserach you can only count the paragraphs.根据我的研究,您只能计算段落。 Of course with some python wizardry I can calculate the amount words and characters but I guess I look for some functionality which in python-pptx might look like this len(text_frame.lines) .当然,通过一些 python 魔法,我可以计算单词和字符的数量,但我想我会寻找一些在python-pptx中可能看起来像这样的功能len(text_frame.lines)

If that functionality is available it would be the icing of the cake.如果该功能可用,那将是锦上添花。 If somebody can do this functionality without using python-pttx or even python I am also interested.如果有人可以在不使用 python-pttx 甚至 python 的情况下执行此功能,我也很感兴趣。

Code snippet looks like this:代码片段如下所示:

for slide in prs.slides: 
    for shape in slide.shapes: 
        text_frame = shape.text_frame
        key=[substring for substring in keys_dict if substring in text_frame.text]
        if 'My_Key' in key:
            text_frame.text=item_to_replace
            print(len(text_frame.paragraphs)) 
            # searched: len(text_frame.lines)

The short answer is "No".最简洁的答案是不”。

The reason why is that python-pptx is essentially a PowerPoint (.pptx) file editor and the number of rendered lines in a text box is not specified in the file.原因是python-pptx本质上是一个 PowerPoint (.pptx) 文件编辑器,并且文件中没有指定文本框中呈现的行数。 The .pptx file specifies the text, how it is divided into paragraphs and runs and the character formatting of each run, but the actual display characteristics, like which letter shows up in what location on the screen or printed page is all determined by the rendering engine . .pptx文件指定了文本,如何划分段落和运行以及每个运行的字符格式,但实际的显示特性,例如哪个字母出现在屏幕或打印页面的哪个位置,都是由渲染决定的发动机

In Microsoft PowerPoint, this is (part of) the PowerPoint application itself.在 Microsoft PowerPoint 中,这是 PowerPoint 应用程序本身的(一部分)。 Other clients like LibreOffice have their own rendering engines. LibreOffice 等其他客户端也有自己的渲染引擎。

As you say, with some fancy programming you can approximate the values you mention, like how many lines it takes up and where the line-breaks occur, but in doing so you are implementing your own (partial) rendering engine.正如您所说,通过一些花哨的编程,您可以近似您提到的值,例如它占用了多少行以及发生换行符的位置,但是这样做您正在实现自己的(部分)渲染引擎。 This of course cannot be guaranteed to always produce the same result as a particular client.这当然不能保证总是产生与特定客户端相同的结果。

Note that not all clients will necessarily render identically.请注意,并非所有客户端都必须以相同的方式呈现。 You wouldn't have to look far to find small differences in how a slide was rendered in Google Docs, for example, vs. PowerPoint.例如,与 PowerPoint 相比,Google Docs 中幻灯片呈现方式的细微差别无需费力寻找。

Other related common questions like "How can I tell when my table is full so I can continue it on the next slide?"其他相关的常见问题,例如“我怎样才能知道我的桌子何时已满,以便在下一张幻灯片上继续?” also cannot be solved without resorting to rendering.不借助渲染也无法解决。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM