pythonptx从幻灯片标题中提取文本

2024-06-02 06:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python构建一个文档检索引擎,它返回根据用户提交的查询的相关性排列的文档。我有一个文档集,其中还包括PowerPoint文件。对于ppt,在结果页面上,我想向用户展示前几个幻灯片标题,以便让他/她有一个更清晰的画面(有点像我们在Google搜索中看到的)。在

所以基本上,我想用python从PPT文件中提取幻灯片标题中的文本。我使用的是python-pptx包。目前我的实现是这样的

from pptx import Presentation
prs = Presentation(filepath) # load the ppt
slide_titles = [] # container foe slide titles
for slide in prs.slides: # iterate over each slide
        title_shape =  slide.shapes[0] # consider the zeroth indexed shape as the title
        if title_shape.has_text_frame: # is this shape has textframe attribute true then
            # check if the slide title already exists in the slide_title container
            if title_shape.text.strip(""" !@#$%^&*)(_-+=}{][:;<,>.?"'/<,""")+ '. ' not in slide_titles: 
                slide_titles.append(title_shape.text.strip(""" !@#$%^&*)(_-+=}{][:;<,>.?"'/<,""")+ '. ')  

但正如你所见,我假设每张幻灯片上的零索引形状是幻灯片标题,这显然不是每次都是这样。有什么办法吗?在

提前谢谢。在


Tags: 文件thetext用户in文档标题if
2条回答

如何从目录中的pptx中提取所有文本(fromthis blog

from pptx import Presentation
import glob

for eachfile in glob.glob("*.pptx"):
    prs = Presentation(eachfile)
    print(eachfile)
    print("           ")
    for slide in prs.slides:
        for shape in slide.shapes:
            if hasattr(shape, "text"):
                print(shape.text)

Slide.shapes(一个SlideShapes对象)具有属性.title,当有一个(通常是)时返回标题形状,如果没有标题,则返回标题形状。
http://python-pptx.readthedocs.io/en/latest/api/shapes.html#slideshapes-objects

这是访问标题形状的首选方法。在

请注意,并非所有幻灯片都有标题形状,因此您必须测试None结果,以避免在这种情况下出现错误。在

另外请注意,用户有时会使用不同的标题形状,比如添加一个单独的新文本框。所以你不能保证你得到的文本“出现”作为标题在幻灯片。但是,您将得到与PowerPoint认为的标题相匹配的文本,例如,在大纲视图中它显示为该幻灯片的标题的文本。在

相关问题 更多 >