【发布时间】: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