Search code examples
pythonpython-3.xpypdf

PyPDF2.utils.PdfReadError: File has not been decrypted


I have been learning Python PyPDF2, This was the code on geeksforgeeks.org/

# importing required modules
import PyPDF2

# creating a pdf file object
pdfFileObj = open('English.pdf', 'rb')

# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

# printing number of pages in pdf file
print(pdfReader.numPages)

# creating a page object
pageObj = pdfReader.getPage(0)

# extracting text from page
print(pageObj.extractText())

# closing the pdf file object
pdfFileObj.close()

After running this program, this error pops up:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1147, in getNumPages
  self.decrypt('')
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1987, in decrypt  
  return self._decrypt(password)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1996, in _decrypt 
  raise NotImplementedError("only algorithm code 1 and 2 are supported")
NotImplementedError: only algorithm code 1 and 2 are supported

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main.py", line 11, in <module>
  print(pdfReader.getNumPages())
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1150, in getNumPages
  raise utils.PdfReadError("File has not been decrypted")
PyPDF2.utils.PdfReadError: File has not been decrypted

I tried different ways to resolve this, but this error stays, can you please guide me here?


Solution

  • PyPDF2 only supports very old PDF files. It doesn't support the formats from Acrobat 6. You'll either need to convert this to an older format or find a different PDF library.

    https://github.com/mstamy2/PyPDF2/issues/378