python - 使用包 pdf2image 的函数 convert_from_path() 时出现 FileNotFoundError

标签 python windows

我正在尝试使用 Python 的库将我的 pdf 文件转换为 png 文件 pdf2image .我使用以下代码转换我的 pdf 文件。

from pdf2image import convert_from_path, convert_from_bytes
pdf_file_path = './samples/my_pdf.pdf'
images = convert_from_path(pdf_file_path)

我想这样做是为了稍后使用 pytesseract 将我的 pdf 文件转换为字符串文本.

我一直遇到的问题是以下 FileNotFound 错误,即使文件位于正确的路径中。谁能帮我弄清楚我做错了什么?

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-9-0b7f9e29e79a> in <module>()
      1 from pdf2image import convert_from_path, convert_from_bytes
      2 pdf_file_path = './samples/my_pdf.pdf'
----> 3 images = convert_from_path(pdf_file_path)

C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\site-packages\pdf2image\pdf2image.py in convert_from_path(pdf_path, dpi, output_folder, first_page, last_page, fmt)
     22     uid, args, parse_buffer_func = __build_command(['pdftoppm', '-r', str(dpi), pdf_path], output_folder, first_page, last_page, fmt)
     23 
---> 24     proc = Popen(args, stdout=PIPE, stderr=PIPE)
     25 
     26     data, err = proc.communicate()

C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    707                                 c2pread, c2pwrite,
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:
    711             # Cleanup if the child failed starting.

C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    995                                          env,
    996                                          os.fspath(cwd) if cwd is not None else None,
--> 997                                          startupinfo)
    998             finally:
    999                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

最佳答案

抱歉回复晚了。

原因

深入研究pdf2image的源代码后,错误是由pdfinfo引起的,它是pdf2image中的一个*nix基本命令> 包裹。因此,当您在缺少 pdfinfo 命令的 Windows 上使用此包时,将导致上述错误。

来自 pdf2image 的代码:

#inside __page_count() function
    ...
    else:
        proc = Popen(["pdfinfo", pdf_path], stdout=PIPE, stderr=PIPE)
    ...

从上面的代码可以看出,它调用了pdfinfo的一个子进程来获取pdf文件的页数。

解决方案

从以下位置下载窗口版 poppler 工具:http://blog.alivate.com.au/poppler-windows/

解压缩并将 bin 的位置(如 C:\somepath\poppler-0.67.0_x86\poppler-0.67.0\bin)添加到您的环境 PATH。

如果你正在打开,请重新启动你的 CMD 和 python virtualenv

关于python - 使用包 pdf2image 的函数 convert_from_path() 时出现 FileNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49508931/

相关文章:

c++ - libodbc++ 处于休眠状态吗?有明确的继任者吗?

python - Discord.py 机器人在嵌入中未使用正确的文本格式,而仅在移动设备上使用?

android - windows - 使用 ndk 构建 tess-two

c++ - 了解 C++ 程序 [ Bjarne Stroustrup 的书 ]

python - Python 中正则表达式的替代品

windows - 如何将字节数组转换为 Windows 8.0 商店应用程序的 ImageSource

c++ - 使用自定义服务器通过 http 安全传输数据

python3.8在mac上尝试git commit到bitbucket时没有这样的文件或目录

python - Python statsmodels ARIMA LinAlgError : SVD did not converge

python - 标记一列中的 ID 第一次出现在另一列中