pdf2image中的路径中有Poppler

83
我正在尝试使用pdf2image,但似乎我需要一个叫做poppler的东西:
(sum_env) C:\Users\antoi\Documents\Programming\projects\summarizer>python ocr.py -i fr13_idf.pdf
Traceback (most recent call last):
  File "c:\Users\antoi\Documents\Programming\projects\summarizer\sum_env\lib\site-packages\pdf2image\pdf2image.py", line 165, in __page_count
    proc = Popen(["pdfinfo", pdf_path], stdout=PIPE, stderr=PIPE)
  File "C:\Python37\lib\subprocess.py", line 769, in __init__
    restore_signals, start_new_session)
  File "C:\Python37\lib\subprocess.py", line 1172, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ocr.py", line 53, in <module>
    pdfspliterimager(image_path)
  File "ocr.py", line 32, in pdfspliterimager
    pages = convert_from_path("document-page%s.pdf" % i, 500)
  File "c:\Users\antoi\Documents\Programming\projects\summarizer\sum_env\lib\site-packages\pdf2image\pdf2image.py", line 30, in convert_from_path
    page_count = __page_count(pdf_path, userpw)
  File "c:\Users\antoi\Documents\Programming\projects\summarizer\sum_env\lib\site-packages\pdf2image\pdf2image.py", line 169, in __page_count
    raise Exception('Unable to get page count. Is poppler installed and in PATH?')
Exception: Unable to get page count. Is poppler installed and in PATH?

我尝试了这个链接,但下载的东西并没有解决我的问题。

3
Iggy,我注意到许多其他人在Windows上使用Poppler时也遇到了类似的问题。因此,我写了一篇简短的文章介绍如何使用WSL解决这个问题。你可以在这里找到这篇文章(Poppler on Windows):https://medium.com/@matthew_earl_miller/poppler-on-windows-179af0e50150 - Matthew E. Miller
17个回答

75

pdf2image只是一个围绕着poppler(不是propeller!)的框架,使用此模块前需要在您的机器上安装并将poppler-utils添加至您的环境变量中。

如何安装该程序的步骤详见项目自述文件的"How to install"章节。


36
在Linux上,安装poppler-utils可以使用命令apt-get install poppler-utils - arun
5
我甚至无法安装Poppler。 - taga
20
在Mac上安装Poppler,需要执行命令brew install poppler - Sajjad Aemmi
pdf2image 无法捆绑 poppler 的原因就像糟糕的一样:因为它将会被 poppler 的“病毒式”版权(GPL)“感染”。许可证 - mirekphd

36

首先从这里下载Poppler,然后解压。在代码部分添加如下内容:poppler_path=r'C:\Program Files\poppler-0.68.0\bin'(例如)。

from pdf2image import convert_from_path
images = convert_from_path("mypdf.pdf", 500,poppler_path=r'C:\Program Files\poppler-0.68.0\bin')
for i, image in enumerate(images):
    fname = 'image'+str(i)+'.png'
    image.save(fname, "PNG")
现在已经完成。通过这个技巧,无需添加环境变量。如果您有任何问题,请告诉我。

或者你可以将poppler_path添加到系统设置的Windows路径环境中,如上所述。别忘了之后重新启动。这样一来,您就不需要将其添加到每个新项目中了。 - 00zetti
@Rajkumar,数字500是什么意思? - YasserKhalil

12

这些 pdf2image 和 pdftotext 库的后端要求是 Poppler,因此您需要安装:

'conda install -c conda-forge poppler'

然后问题就会解决。 如果仍然无法使用,则可以按照http://blog.alivate.com.au/poppler-windows/上的说明安装该库。


1
这个已经不再维护了。在这里下载:https://github.com/oschwartz10612/poppler-windows - Owen Schwartz
对我有用。我使用的是Mac。谢谢! - Suraj
这就是我所要做的,不需要指定poppler的路径,我使用带有conda的mac。 - ProfessorPorcupine

11

pdf2image需要Poppler

在使用pdf2image时,需要满足以下依赖项:

  1. 安装pdf2image

    pip install pdf2image

  2. 安装python-dateutil

    pip install python-dateutil

  3. 安装Poppler

  4. 将Poppler路径指定为环境变量(系统路径)中的一部分

在Windows上安装Poppler

将Poppler添加到路径中

  • 将Poppler安装到位置:C:\Users\UserName\Downloads\Release-21.11.0-0.zip
  • 将C:\Users\UserName\Downloads\Release-21.11.0-0.zip添加到环境变量中的系统变量路径

在代码中指定poppler路径

pages = convert_from_path(filepath, poppler_path=r"actualpoppler_path")

9

出现问题的是 poppler 安装不正确,可以使用以下命令获取正确的安装包并进行安装。

sudo apt-get install poppler-utils


6

对于Windows操作系统;解决PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?问题的方法:


1
为了安装Choco,请以Powershell管理员身份运行以下命令:Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) - Muneeb Ahmad Khurram

3

如果在Windows上仍有此错误,可通过以下方式解决:

  • Poppler for Windows下载最新的Windows二进制文件。
  • 将其解压到C盘下,例如C:\poppler-0.68.0
  • 像这样指定Poppler路径:
from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path
import os

ROOT_DIR = os.path.abspath(os.curdir)

# Path of the pdf 
PDF_file = ROOT_DIR + r"\PdfToImage\src\2.pdf"
  
''' 
Part #1 : Converting PDF to images 
'''
  
# Store all the pages of the PDF in a variable 
pages = convert_from_path(PDF_file, 500, poppler_path=r'C:\poppler-0.68.0\bin')

我按照这些步骤操作,但仍然出现“无法获取页面计数。是否已安装Poppler并添加至PATH?”的提示信息。 - Francesco Pettini

3

在Windows中

安装Poppler for Windows Poppler

  • 500 = Quality of JPG

  • the path contains the pdf files

  • pip install pdf2img

     path = r'C:\ABC\FEF\KLH\pdf_extractor\output\break'
    
     def spliting_pdf2img( path):
         from pdf2image import convert_from_path, convert_from_bytes
         for file in os.listdir(path):
             if file.lower().endswith(".pdf"):
                 pages = convert_from_path(os.path.join(path,file), 500,poppler_path= r'C:\ABC\DEF\Downloads\poppler-0.68.0\bin')
                 for page in pages:                    
                     page.save(os.path.join(path,file.lower().replace(".pdf",".jpg")),'JPEG')    
    

在Linux/Ubuntu中 请在终端中安装以下软件包:

  • sudo apt-get update

  • sudo apt-get install poppler-utils

    path = r'C:\ABC\FEF\KLH\pdf_extractor\output\break'
    
     def spliting_pdf2img( path):
         from pdf2image import convert_from_path, convert_from_bytes
         for file in os.listdir(path):
             if file.lower().endswith(".pdf"):
                 pages = convert_from_path(os.path.join(path,file), 500)
                 for page in pages:                    
                     page.save(os.path.join(path,file.lower().replace(".pdf",".jpg")),'JPEG')
    

2

如果你在使用MAC系统,并且已经安装了brew,那么这就是最好的选择。

brew install poppler

安装所有依赖项需要几分钟时间,但安装完成后pdf2image将正常工作。
这是一个重复的答案,可以在这里找到答案,也可以在本页面的评论中找到答案。我添加这个答案,因为我花了一些时间才找到MAC的正确解决方案。

错误:无法在Intel默认前缀(/usr/local)上的ARM处理器中在Homebrew中安装! (针对Mac M1) - territorial
是的,我记得在使用Homebrew时遇到了一些M1问题。这是相当普遍的问题,有几种解决方法。这里列出了一些解决方案:https://dev59.com/X1EG5IYBdhLWcg3wgf2Y。 - MattC

0

我在 Mac 上使用 Visual Studio Code 和 conda 环境遇到了同样的问题。

后来我发现我可以通过命令行运行代码,但不能在 VS Code 中运行。然后我使用以下方法在命令行和 VS Code 中打印了环境变量:

print(os.environ)

当我对比两者时,我注意到“PATH”变量不同。我的conda环境在VS code中的“PATH”变量中不存在。我想这意味着VS code没有正确激活我的conda环境。因此,我从命令行中获取了我的“PATH”,并将其设置为我的launch.json环境变量。然后问题就解决了。
"configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "python": "/Users/<username>/miniconda3/envs/<env_name>/bin/python",
            "env": {
                "PATH":"<PATH STRING from command line>"
            },
            "program": "${file}"
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接