python读取带密码的pdf_Python从受密码保护的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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值