我写了一个函数,可以将目录中的每个pdf转换为文本,我想从pdf中获取转换后的文本作为txt文件。我收到"类型错误:预期的 str、字节或 os。路径类对象,而不是元组"错误在我的代码中。谁能帮我解决这个问题。在此处附加代码:
import io
import os
import os.path
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
def extract_text_from_pdf(pdf_path):
resource_manager = PDFResourceManager()
fake_file_handle = io.BytesIO()
converter = TextConverter(resource_manager, fake_file_handle)
page_interpreter = PDFPageInterpreter(resource_manager, converter)
with open(pdf_path, 'rb') as fh:
for page in PDFPage.get_pages(fh,
caching=True,
check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
# close open handles
converter.close()
fake_file_handle.close()
if text:
return text
def save_to_txt(lst):
for i, ele in enumerate(lst):
txtfile = "{}.txt".format(i)
files = extract_text_from_pdf(ele)
with open(txtfile, "w") as textfile:
textfile.write(files)
if __name__ == '__main__':
pdf_path = 'C:\Users\Lenovo\.spyder-py3\OCR'
for root, _, files in os.walk(pdf_path):
for filename in files:
filepath = os.path.join(root, filename)
extract_text_from_pdf(filepath)
for f in filepath:
save_to_txt(f)
错误如下:
runfile('C:/Users/Lenovo/.spyder-py3/updatedpy.py', wdir='C:/Users/Lenovo/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-17-f6b3bb00c382>", line 1, in <module>
runfile('C:/Users/Lenovo/.spyder-py3/updatedpy.py', wdir='C:/Users/Lenovo/.spyder-py3')
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesspyderutilssitesitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:Program Files (x86)Microsoft Visual StudioSharedAnaconda3_64libsite-packagesspyderutilssitesitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Lenovo/.spyder-py3/updatedpy.py", line 47, in <module>
extract_text_from_pdf(file)
File "C:/Users/Lenovo/.spyder-py3/updatedpy.py", line 22, in extract_text_from_pdf
with open(pdf_path, 'rb') as fh:
TypeError: expected str, bytes or os.PathLike object, not tuple
错误来自在主部分中使用 os.walk 方法,该方法不返回文件名,而是返回元组。有关更多详细信息,请参阅操作系统文档。
编辑:你可以像这样使用os.walk方法:
for root, _, files in os.walk(pdf_path):
for filename in files:
filepath = os.path.join(root, filename)
extract_text_from_pdf(filepath)
或者,您可以使用 path.py 库并使用 walkfiles 方法。这样你就可以做到:
from path import Path
pdf_path = Path('C:\dev')
for file in pdf_path.walkfiles():
extract_text_from_pdf(file)