Unable to use pypdf module

故事扮演 提交于 2021-02-08 12:16:22

问题


I have installed the pyPdf module successfully using the command pip install pydf but when I use the module using the import command I get the following error:

enC:\Anaconda3\lib\site-packages\pyPdf\__init__.py in <module>()
1 from pdf import PdfFileReader, PdfFileWriter
  2 __all__ = ["pdf"]
ImportError: No module named 'pdf'

What should I do? I have installed the pdf module as well but still the error does not go away.


回答1:


This is a problem of PyPDF, which does not occur in PyPDF2. Actually, the official pyPdf page recommends using PyPDF2.

Install PyPDF2

$ sudo -H pip install PyPDF2

You might need to replace pip by pip2 or pip3 if you use Python 2 or Python 3.

Use PyPDF2

import PyPDF2

Moving from pyPdf to PyPDF2

Simply replace all occurrences of pyPdf by PyPDF2.

WARNING: PyPDF, PyPDF2, PyPDF3, PyPDF4 are all not maintained!

Three potential alternatives which are maintained:

  • pymupdf: uses mupdf
  • pikepdf: Uses qpdf
  • pdfminer.six: A pure Python project. Don't confuse it with the unmaintained pdfminer



回答2:


I've had the same error popping up after installing pypdf via pip and trying to import it in IPython (I'm using python 3.5.2):

In [5]: import pyPdf
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-a5780a4295f9> in <module>()
----> 1 import pyPdf

/home/mf/virtual_envs/pdfdataextract/lib/python3.5/site-packages/pyPdf/__init__.py in <module>()
----> 1 from pdf import PdfFileReader, PdfFileWriter
      2 __all__ = ["pdf"]

ImportError: No module named 'pdf'

This was even after installing the pdf library using pip.

Luckily, there's a PyPDF2 library which works like a charm for me.




回答3:


Use PyPDF2.
I've been using it in Python 3 (v3.5.2 to be precise), and it works quite well.
Here's a simple command that you can use to install PyPDF2.

sudo -H pip3 install PyPDF2

For using it:

from PyPDF2 import PdfFileReader

Let me know if you need any clarification.




回答4:


Firstly, in your code you wrote:

from pdf import PdfFileReader, PdfFileWriter

Instead of:

from PyPDF2 import PdfFileReader, PdfFileWriter

Secondly use

 pip3.x install pyPdf

instead of pip install pyPdf if it will not work




回答5:


I use pypdf2 , it work for me. pip install pypdf2. I use Ubuntu 16.04




回答6:


Your import code should read:

from pyPdf import PdfFileReader, PdfFileWriter


来源:https://stackoverflow.com/questions/42130504/unable-to-use-pypdf-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!