9

I would like to read the pdf file. This is a book.pdf with a password (256 bit AES encryption). I know a password. I am using Jupyter Notebook.

I get an error:

import PyPDF2
reader = PyPDF2.PdfFileReader('book.pdf')
reader.decrypt('333')
reader.getPage(0)


---------------------------------------------------------------------------
 NotImplementedError                       Traceback (most recent call last)
 <ipython-input-12-7dd54b6a760a> in <module>()
  1 import PyPDF2
  2 reader = PyPDF2.PdfFileReader('book.pdf')
  ----> 3 reader.decrypt('333')
  4 reader.getPage(0)

 c:\users\a\programs\python\python36-32\lib\site-packages\PyPDF2\pdf.py in 
 decrypt(self, password)
 1985         self._override_encryption = True
 1986         try:
 -> 1987             return self._decrypt(password)
 1988         finally:
 1989             self._override_encryption = False

 c:\users\a\python\python36-32\lib\site-packages\PyPDF2\pdf.py in 
_decrypt(self, password)
1994             raise NotImplementedError("only Standard PDF encryption 
handler is available")
1995         if not (encrypt['/V'] in (1, 2)):
-> 1996             raise NotImplementedError("only algorithm code 1 and 2 
are supported")
1997         user_password, key = self._authenticateUserPassword(password)
1998         if user_password:

NotImplementedError: only algorithm code 1 and 2 are supported
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
batmanforever
  • 155
  • 1
  • 2
  • 9
  • https://github.com/mstamy2/PyPDF2/issues/53 – user3483203 Jun 07 '18 at 23:03
  • https://github.com/mstamy2/PyPDF2/issues/378 – user3483203 Jun 07 '18 at 23:04
  • 2
    Also see https://github.com/mstamy2/PyPDF2/issues/385 for status. Basically, PyPDF2 has been dead for 3 years; after half a year of discussion on resuming maintenance, it looks like the people involved have decided to abandon it and begin work on a [PyPDF3](https://github.com/mstamy2/PyPDF3) project, but little to nothing has been done on that yet. So, the answer is either (a) wait until PyPDF3 is ready for prime time (if ever), (b) switch to a different PDF library, or (c) use workarounds like subprocessing out to command-line tools for steps PyPDF2 can't handle, like decrypting algorithm 4. – abarnert Jun 07 '18 at 23:12

4 Answers4

7

Recently, I also faced the same issue. I am not sure why the error occurs, but here is a way to mitigate it, using a different module than PyPDF2 :

import pikepdf
pdf = pikepdf.open('book.pdf',password='333')
pdf.save('book_without_pass.pdf')

The above code save the encrypted pdf book.pdf with the password, '333' to book_without_pass.pdf

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
1

I had the same issue, then changed PDF options on Encryption Level: 40 bit RC4 and it helped. I think it relates with PyPDF2 module.

Dmitrii P.
  • 11
  • 1
1

NotImplementedError This is because module does not support the files encrypted format
You have to try different modules

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
1

Meanwhile, the necessary functionality has been added in PyPDF2.

I tested it in version 3.0.1. If you arrived here via a search on the error message, you should check whether the latest PyPDF2 version fixes your problem.

For historical background, see the comment that abarnert posted under the question.

pj.dewitte
  • 472
  • 3
  • 10