Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get number of pages from password protected pdf

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?

like image 629
JBeardie Avatar asked Nov 08 '22 17:11

JBeardie


1 Answers

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

like image 195
Kallz Avatar answered Nov 15 '22 13:11

Kallz