【发布时间】:2021-04-07 23:17:22
【问题描述】:
我正在尝试将许多 pdf 文件转换为 txt。我的 pdf 文件组织在目录中的子目录中。所以我有三层:目录-->子目录-->每个子目录中有多个pdf文件。我正在使用以下代码,这给了我这个错误ValueError: too many values to unpack (expected 3)。当我转换单个目录中的文件而不是多个子目录中的文件时,该代码有效。
这可能很简单,但我无法理解它。任何帮助将非常感激。谢谢。
import pytesseract
from pdf2image import convert_from_path
import glob
pdfs = glob.glob(r"K:\pdf_files")
for pdf_path, dirs, files in pdfs:
for file in files:
convert_from_path(os.path.join(pdf_path, file), 500)
for pageNum,imgBlob in enumerate(pages):
text = pytesseract.image_to_string(imgBlob,lang='eng')
with open(f'{pdf_path}.txt', 'a') as the_file:
the_file.write(text)
【问题讨论】:
-
您正在寻找
os.walk,而不是glob.glob。 -
感谢@Tim Roberts。现在我收到此错误
PDFPageCountError: Unable to get page count. I/O Error: Couldn't open file 'K:\pdf_files': No error. -
谢谢@aneroid 仍然收到错误
PDFPageCountError: Unable to get page count. I/O Error: Couldn't open file '000020051-20140528122047.pdf': No error.现在它无法打开pdf文件
标签: python python-3.x pdf python-tesseract