我正在尝试寻找一种方法来查看一个文件夹并搜索其中所有幻灯片文档的内容,以查找特定的字符串,最好使用Python。当找到这些字符串时,我希望报告该字符串之后的文本以及在其中发现的文档。我想编译信息并在CSV文件中报告。
到目前为止,我只找到了olefil包,https://bitbucket.org/decalage/olefileio_pl/wiki/Home。它提供了特定文档中包含的所有文本,这不是我要做的。请帮忙。
我正在尝试寻找一种方法来查看一个文件夹并搜索其中所有幻灯片文档的内容,以查找特定的字符串,最好使用Python。当找到这些字符串时,我希望报告该字符串之后的文本以及在其中发现的文档。我想编译信息并在CSV文件中报告。
到目前为止,我只找到了olefil包,https://bitbucket.org/decalage/olefileio_pl/wiki/Home。它提供了特定文档中包含的所有文本,这不是我要做的。请帮忙。
如果你想提取文本:
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)
PackageNotFoundError
错误,可以通过传递文件对象来修复它:f = open (<filepath>,“rb”)
,然后 prs = Presentation(f)
。 - Viseshini Reddytika-python
Python版本的Apache Tika库,根据文档,Apache Tika支持从超过1500个文件格式中提取文本。
注意: 它还可以与 pyinstaller 非常好地兼容。
使用pip安装:
pip install tika
示例:
#!/usr/bin/env python
from tika import parser
parsed = parser.from_file('/path/to/file')
print(parsed["metadata"]) #To get the meta data of the file
print(parsed["content"]) # To get the content of the file
官方链接到GitHub
python-pptx
可以用来实现你所提出的功能。大致上,你需要像这样做(以下是整体思路,不是可用代码):
from pptx import Presentation
for pptx_filename in directory:
prs = Presentation(pptx_filename)
for slide in prs.slides:
for shape in slide.shapes:
print shape.text
您需要添加有关搜索形状文本以查找关键字符串并将其添加到CSV文件或其他文件的部分,但是这种一般方法应该完全可行。我会留给您处理更细节的问题 :)
if hasattr(shape, 'text'): print(shape.text)
- Ray WalkerTextract-Plus
使用 textract-plus,它可以从大多数文档扩展名中提取文本,包括 pptx 和 pptm。参考文档
安装-
pip install textract-plus
示例-
import textractplus as tp
text=tp.process('path/to/yourfile.pptx')
针对您的情况 -
import os
import pandas as pd
import textractplus as tp
files_csv=[]
your_dir='.'
for f in os.listdir(your_dir):
if f.endswith('pptx') or f.endswith('pptm'):
text=tp.process(os.join(your_dir,f))
files_csv.append([f,text])
pd.Dataframe(files_csv,columns=['filename','text']).to_csv('your_csv.csv')
这段代码将从目录中获取所有的pptx和pptm文件,并创建一个CSV文件,其中第一列为文件名,第二列为从该文件中提取的文本。
import os
import textract
files_csv = []
your_dir = '.'
for f in os.listdir(your_dir):
if f.endswith('pptx') or f.endswith('pptm'):
text = tp.process(os.path.join('sample.pptx'))
print(text)