我有一个由VBA调用的python脚本,它循环遍历excel工作簿中的任何文件夹,然后返回该文件夹中的PDF (因为只有一个),然后打开它并返回文本;然而,我得到以下错误,我不知道我做错了什么:
Traceback (most recent call last):
File "C:\Users\Path...", line 16, in <module>
with pdfplumber.open(pdf_file) as pdf:
File "C:\ProgramData\Anaconda3\lib\site-packages\pdfplumber\pdf.py", line 54, in open
fp = open(path_or_fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Company - Terms - Jan 01.pdf'
代码:
"""Declare Variables"""
FILE_PATH = 'C:/Users/Path...'
dir_list = os.listdir(FILE_PATH)
pdf_file = ''
'Find the PDF in the folder'
for file in dir_list:
file_type = file[file.find('.'):len(file)]
if file_type == '.pdf':
pdf_file = file
with pdfplumber.open(pdf_file) as pdf:
page = pdf.pages[0]
text = page.extract_text()
continue
print(text)
发布于 2022-02-01 18:36:38
我认为问题在于,您没有将pdf文件的完整路径传递给pdfplumber.open()
。我建议您在需要处理文件和目录文件夹的路径操作时使用pathlib
模块。这样做会使您的代码看起来如下:
from pathlib import Path
FOLDER_PATH = Path('C:/Users/Path...')
# Find the PDFs in the folder
for pdf_file in FOLDER_PATH.glob('*.pdf'):
with pdfplumber.open(str(pdf_file)) as pdf:
page = pdf.pages[0]
text = page.extract_text()
print(text)
注意,您可能不需要str(pdf_file)
,这取决于pdfplumber.open()
是否接受Path
对象作为参数(现代的Path
软件应该这样做)。
https://stackoverflow.com/questions/70945410
复制相似问题