Search code examples
pythonpython-3.xpippackagepypdf

ImportError: cannot import name 'PdfReader' from 'PyPDF2'


I installed the PyPDF2 package using pip and got the following message after the installation:

!pip install PyPDF2
Collecting PyPDF2
  Downloading PyPDF2-2.11.1-py3-none-any.whl (220 kB)
     -------------------------------------- 220.4/220.4 kB 2.7 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions>=3.10.0.0 in c:\users\dell\appdata\local\programs\python\python37\lib\site-packages (from PyPDF2) (3.10.0.2)
Installing collected packages: PyPDF2
Successfully installed PyPDF2-2.11.1
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
camelot-py 0.10.1 requires openpyxl>=2.5.8, which is not installed.

I imported PdfReader and ran the following code:

from PyPDF2 import PdfReader

reader = PdfReader(r"D:\Sample.pdf")
for page in reader.pages:
    for image in page.images:
        with open(image.name, "wb") as fp:
            fp.write(image.data)

But, I get an "ImportError":

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_19816/3180674727.py in <module>
----> 1 from PyPDF2 import PdfReader
      2 
      3 reader = PdfReader(r"D:\Sample.pdf")
      4 for page in reader.pages:
      5     for image in page.images:

ImportError: cannot import name 'PdfReader' from 'PyPDF2' (C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\__init__.py)

Any inputs on how to resolve this issue?


Solution

  • The question was about PyPDF2. I installed the package using pip, but then I wasn't able to import the PdfReader module from it. During the installation as you can see, I overlooked an error it threw, saying that "pip's dependency resolver does not currently take into account all the packages that are installed". Then when @SembeiNorimaki, in the above comments, pointed out that I have to install openpyxl>=2.5.8. I looked upon the error and resolved its dependency conflict with PyPDF2 by installing the latest version of openpyxl.

    Hope this clears any confusion you have!