在多个 powerpoint 文件中查找单词 Python
Find a word in multiple powerpoint files Python
我在一个目录中有很多 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
预期的结果是在其中一张幻灯片中找到单词 "data" 并打印 true。实际上预期的结果是:
Person1.pptx
----------------------
Exist? True
Person1.pptx
----------------------
Exist? False
如果在每张幻灯片的任何形状中都存在该词,则为真;如果该词在幻灯片的所有形状中都不存在,则为假。
我自己找的。 :)
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
回答这个问题,因为上面的回答可能比我误导更多。它不完整。这都没错。但在许多现实生活中,它会产生错误的结果。
问题是它忽略了有许多结构要解析。上面的代码仅解析其中的一些(本身直接带有文本的形状)。最重要的结构也需要被解析以找到所有带有所需文本的形状,就是组。这是一个本身可能不包含文本的形状,但可能包含包含文本的形状。
此外,这个组形状或其形状可能又包含其他组。这导致我们需要迭代搜索策略。因此,在分析每张幻灯片中的形状时需要一种不同的方法。这最好通过重用上面的代码来显示,保留第一部分:
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
为了完全按照预期工作,中断需要以不同方式处理(中断所有正在进行的循环)。这会使代码有点复杂,并且会错过对组解析的关注,因此在这里忽略。
我在一个目录中有很多 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
预期的结果是在其中一张幻灯片中找到单词 "data" 并打印 true。实际上预期的结果是:
Person1.pptx
----------------------
Exist? True
Person1.pptx
----------------------
Exist? False
如果在每张幻灯片的任何形状中都存在该词,则为真;如果该词在幻灯片的所有形状中都不存在,则为假。
我自己找的。 :)
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
回答这个问题,因为上面的回答可能比我误导更多。它不完整。这都没错。但在许多现实生活中,它会产生错误的结果。
问题是它忽略了有许多结构要解析。上面的代码仅解析其中的一些(本身直接带有文本的形状)。最重要的结构也需要被解析以找到所有带有所需文本的形状,就是组。这是一个本身可能不包含文本的形状,但可能包含包含文本的形状。
此外,这个组形状或其形状可能又包含其他组。这导致我们需要迭代搜索策略。因此,在分析每张幻灯片中的形状时需要一种不同的方法。这最好通过重用上面的代码来显示,保留第一部分:
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
为了完全按照预期工作,中断需要以不同方式处理(中断所有正在进行的循环)。这会使代码有点复杂,并且会错过对组解析的关注,因此在这里忽略。