首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试用PDFPlumber结果在FileNotFoundError中打开PDF

尝试用PDFPlumber结果在FileNotFoundError中打开PDF
EN

Stack Overflow用户
提问于 2022-02-01 18:06:14
回答 1查看 635关注 0票数 0

我有一个由VBA调用的python脚本,它循环遍历excel工作簿中的任何文件夹,然后返回该文件夹中的PDF (因为只有一个),然后打开它并返回文本;然而,我得到以下错误,我不知道我做错了什么:

代码语言:javascript
运行
复制
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'

代码:

代码语言:javascript
运行
复制
"""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)
EN

回答 1

Stack Overflow用户

发布于 2022-02-01 18:36:38

我认为问题在于,您没有将pdf文件的完整路径传递给pdfplumber.open()。我建议您在需要处理文件和目录文件夹的路径操作时使用pathlib模块。这样做会使您的代码看起来如下:

代码语言:javascript
运行
复制
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软件应该这样做)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70945410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档