Jin Lee Jin Lee - 12 days ago 6
Python Question

PyPDF 2 Decrypt Not Working

Currently I am using the PyPDF 2 as a dependency.

I have encountered some encrypted files and handled
them as you normally would (in the following code):

PDF = PdfFileReader(file(pdf_filepath, 'rb'))
if PDF.isEncrypted:
PDF.decrypt("")
print PDF.getNumPages()


My filepath looks something like "~/blah/FDJKL492019 21490 ,LFS.pdf"
PDF.decrypt("") returns 1, which means it was successful. But when it hits print PDF.getNumPages(),
it still raises the error, "PyPDF2.utils.PdfReadError: File has not been decrypted".

How do I get rid of this error?
I can open the PDF file just fine by double click (which default-opens with Adobe Reader).

Answer

To Answer My Own Question: If you have ANY spaces in your file name, then PyPDF 2 decrypt function will ultimately fail despite returning a success code. Try to stick to underscores when naming your PDFs before you run them through PyPDF2.

For example,

Rather than "FDJKL492019 21490 ,LFS.pdf" do something like "FDJKL492019_21490_,LFS.pdf".