[英]Extracting text from multiple powerpoint files using python
我正在尝试找到一种方法来查看文件夹并在该文件夹中的所有 powerpoint 文档的内容中搜索特定的字符串,最好使用 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)
蒂卡蟒蛇
Apache Tika 库的 Python 端口,根据文档,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 文件或其他内容的信息,但这种通用方法应该可以正常工作。 我会把它留给你来解决更精细的问题:)
Textract-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)
如何将上述输出导出到数据框中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.