在一个目录中有一堆带有文本的 PDF 文件。我的想法是能够一次阅读所有这些内容并保存在字典中。现在我只能通过使用 texttract
库来做到这一点,如下所示:
import textract
text = textract.process('/Users/user/Documents/Data/CLAR.pdf',
method='tesseract',
language='eng')
如何才能一次性读取它们?我是否需要使用 for
循环在目录中进行搜索或以其他方式进行搜索?
最佳答案
一种解决方案可能是将os库
与for循环
结合使用
import os
import textract
files_path = [os.path.abspath(x) for x in os.listdir()]
# Excluding not .pdf files
files_path = [pdf for pdf in files_path if '.pdf' in pdf]
pdfs = []
for file in files_path:
text = textract.process(file,
method='tesseract',
language='eng')
pdfs += [text]
- 获取当前目录下的所有文件
- 排除非
.pdf
文件 - 将文本保存到列表中(可以是不同的数据结构)
关于python - 用python读取多个pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678665/