【问题标题】:Find a word in multiple powerpoint files Python在多个 powerpoint 文件中查找一个单词 Python
【发布时间】:2019-04-03 14:37:17
【问题描述】:

我有很多 pptx 文件要在一个目录中搜索,我正在这些文件中寻找特定的词 "data"。我创建了下面的代码来读取所有文件,但它没有提供 true 或 false 的正确结果。例如在Person1.pptx 中,单词“data” 存在于两个“shapes” 中。问题是错误到底出在哪里以及为什么代码的结果不正确。

from pptx import Presentation
import os
files = [x for x in os.listdir("C:/Users/../Desktop/Test") if x.endswith(".pptx")]
for eachfile in files:
    prs = Presentation("C:/Users/.../Desktop/Test/" + eachfile)
    print(eachfile)
    print("----------------------")
    for slide in prs.slides:
        for shape in slide.shapes:
            print ("Exist? " + str(hasattr(shape, 'data')))

结果如下

Person1.pptx
----------------------
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Person2.pptx
----------------------
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False
Exist? False

预期的结果是在其中一张幻灯片中找到“数据”一词并打印为真。实际上预期的结果是:

Person1.pptx
----------------------
Exist? True

Person1.pptx
----------------------
Exist? False

如果在每张幻灯片的任何形状中存在该单词,则为真,如果在幻灯片的所有形状中不存在该单词,则为假。

【问题讨论】:

    标签: python-3.x file text powerpoint extraction


    【解决方案1】:

    我自己找到的。 :)

    from pptx import Presentation
    import os
    
    files = [x for x in os.listdir("C:/Users/.../Desktop/Test") if x.endswith(".pptx")] 
    
    for eachfile in files:
        prs = Presentation("C:/Users/.../Desktop/Test/" + eachfile) 
        for slide in prs.slides:
            for shape in slide.shapes:
                if hasattr(shape, "text"):
                    shape.text = shape.text.lower()
                    if "whatever_you_are_looking_for" in shape.text:
                        print(eachfile)
                        print("----------------------")
                        break
    

    【讨论】:

      【解决方案2】:

      回答这个问题,因为上述答案可能比我误导更多。它并不完整。这也没有错。但在许多现实生活中,它会产生错误的结果。

      问题在于它忽略了要解析的许多结构。上面的代码只解析了其中的一些(直接带有文本的形状)。最重要的结构是组,它也需要解析以找到所需文本的所有形状。这是一个本身可能不包含文本的形状,但可能包含包含文本的形状。

      此外,此组形状或其形状可能又包含其他组。这导致我们需要迭代搜索策略。因此,在解析每张幻灯片中的形状时需要采用不同的方法。这最好通过重用上面的代码来显示,保留第一部分:

      from pptx import Presentation
      from pptx.enum.shapes import MSO_SHAPE_TYPE
      
      import os
      
      files = [x for x in os.listdir("C:/Users/.../Desktop/Test") if x.endswith(".pptx")] 
      
      for eachfile in files:
          prs = Presentation("C:/Users/.../Desktop/Test/" + eachfile) 
          for slide in prs.slides:
      

      那么我们需要将“hasattr”测试替换为对递归部分的调用:

              checkrecursivelyfortext(slide.shapes)
      

      并且还插入函数的新递归函数定义(如在 import 语句之后)。为了便于比较,插入的函数使用与上面相同的代码,只是添加了递归部分:

      def checkrecursivelyfortext(shpthissetofshapes):
          for shape in shpthissetofshapes:
              if shape.shape_type == MSO_SHAPE_TYPE.GROUP:
                  checkrecursivelyfortext(shape.shapes)
              else:
                  if hasattr(shape, "text"):
                      shape.text = shape.text.lower()
                      if "whatever_you_are_looking_for" in shape.text:
                          print(eachfile)
                          print("----------------------")
                          break
      

      要完全按照需要工作,需要以不同方式处理中断(中断所有正在进行的循环)。这会使代码稍微复杂化,并忽略对组解析的关注,因此在这里忽略。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多