Python-pptx - 句子在多行打印时被拆分

Python-pptx - Sentence are getting split while print in multiple lines

我正在从 .pptx 打印,但单个句子从某处分成新行..这是幻灯片的屏幕截图..

阅读以下代码时.. 从 pptx 导入演示文稿

prs = Presentation(path_to_presentation)
for slide in prs.slides:
      for shape in slide.shapes:
            if not shape.has_text_frame:
                continue
            for paragraph in shape.text_frame.paragraphs:
                for run in paragraph.runs:
                    print(run.text)

得到如下输出...

Books include:
Learning Python 
by Mark Lutz
Python Essential Reference 
by David Beazley
Python Cookbook
, ed. by Martelli, Ravenscroft and Ascher
(online at http://code.activestate.com/recipes/langs/python/)
http://wiki.python.org/moin/PythonBooks

您可以比较 pptx 的屏幕截图和 pptx 的打印文本,项目符号点被分成两个或更多句子..就像 "Learning Python by Mark Lutz" 打印在 2 个点 "Learning Python" 和 "by Mark Lutz" 连子弹都漏掉了。

如何解决这个问题?

简短的回答是使用 paragraph.text 而不是 run.text:

for paragraph in shape.text_frame.paragraphs:
    print(paragraph.text)

段落是连贯的文本块,在页边空白之间流动,没有垂直中断。这是 user 的区别,因为它会影响我们阅读内容的方式。 运行 是一系列具有相同字符格式(即字体,但包括粗体、斜体等)的字符。 运行 是 技术上的 区别,因为它们的边界对于 reader 不应该是显而易见的;它们只是用来告诉 PowerPoint "apply this character formatting to all these characters".

如果您单独打印每个 运行,它们会在段落中看似随机的地方中断,这至少取决于斜体打开和关闭的位置,但也经常出现在其他地方,比如有人编辑的地方添加几个字符。 PowerPoint 不一定会减少 运行 的数量,即使两个连续的 运行 具有相同的格式。因此,它们往往会扩散。