2016-11-26 65 views
1

我想要使用Python获取每个幻灯片的PowerPoint演示文件的标题。我在Python中使用Presentation包,但找不到指定标题的任何内容。 我有这个代码,返回的Powerpoint文件的内容。但我需要指定标题。使用Python获得pptx文件幻灯片的标题

from pptx import Presentation 

prs = Presentation("pp.pptx") 

# text_runs will be populated with a list of strings, 
# one for each text run in presentation 
text_runs = [] 

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: 
       text_runs.append(run.text) 
+0

文档建议您可以访问Shapes集合了幻灯片和检测每个形状的类型。如果它是一个Type 14,它将是一个居中的标题占位符。 –

+0

@RadLexus试试我的解决方案 – eyllanesc

+1

该文档专门提到它[这里](http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#setting-the-slide-title)。这有点棘手,因为幻灯片标题形状是一个*占位符*,所以提及它是在占位符被引入之后。这也是从“幻灯片标题”搜索返回的第一个条目。 – scanny

回答

2

这是我的解决方案:

from pptx import Presentation 

filename = path_of_pptx 

prs = Presentation(filename) 

for slide in prs.slides: 
    title = slide.shapes.title.text 
    print(title) 

输入:

enter image description here

输出:

Hello, World! 
Hello, World2! 
Hello, World3! 
+0

谢谢!非常感谢你 – Minerva