Parsifal
Parsifal

Reputation: 342

pypdf2 is not defined

new to python 3.7 version. Trying to use pypdf2 but I have an error that I can't fix by myself:

my comand:

pdfFile2 = open(r"C:\Users\Luca\Desktop\python\tutorial\doc2.pdf", "wb")  # w=write, r=read, b=binary
writer1 = pyPDF2.PdfFileWriter()

The error: Traceback (most recent call last): File "C:/Users/Luca/Desktop/python/tutorial/tutorial.py", line 8, in <module> writer1 = pyPDF2.PdfFileWriter()

NameError: name 'pyPDF2' is not defined

I have installed the pypdf2 library but I cant go on, how can I fix this?

Upvotes: 1

Views: 9984

Answers (2)

Skanda Shastry
Skanda Shastry

Reputation: 302

Step 1: PyPDF2 is a pure Python package, so you can install it using pip (assuming pip is in your system’s path):

python -m pip install pypdf2

Step 2: Once you install that packages. You can import the specific packages like PdfFileReader & PdfFileWriter from that library.

from PyPDF2 import PdfFileReader, PdfFileWriter

Step 3: Finally, you can instantized that module object directly

# For Reader

reader=PdfFileReader(open("fpath",'rb'))

# For Write

writer=PdfFileWriter()
outfp=open("outpath",'wb')
writer.write(outfp)

Doc: https://pythonhosted.org/PyPDF2/PdfFileWriter.html

Upvotes: 3

abolotnov
abolotnov

Reputation: 4322

This is happening most likely because your example uses pyPDF2 (small p) instead of PyPDF2(capital P) in the second line of your code.

Upvotes: 1

Related Questions