I've been trying to figure out a way to get the number of pages from password protected pdf with python3. So far I have tried modules pypdf2 and pdfminer2. Both are failing because the file is not decrypted.
#!/usr/bin/python3
from PyPDF2 import PdfFileReader
pdfFile = PdfFileReader(open("document.pdf", "rb"))
print(pdfFile.numPages)
This code will produce an Error:
PyPDF2.utils.PdfReadError: File has not been decrypted
Is there a way to get the number of pages without decrypting?
You can use pdfrw
Example,
a.pdf and b.pdf are same pdf. Difference is b.pdf is password protected pdf and a.pdf is simple pdf without any protection and no of pages are 30
>>> from pdfrw import PdfReader
>>> print len(PdfReader('b.pdf').pages)
30
>>> print len(PdfReader('a.pdf').pages)
30
For install use following command
pip install pdfrw
For in detail PDFRW
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With