Python从受密码保护的pdf获取页面数 [英] Python get number of pages from password protected pdf

查看:348
本文介绍了Python从受密码保护的pdf获取页面数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种方法,可以使用python3从受密码保护的pdf中获取页数.到目前为止,我已经尝试了pypdf2和pdfminer2模块. 两者均因未解密文件而失败.

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)

此代码将产生错误:

PyPDF2.utils.PdfReadError: File has not been decrypted

是否有一种无需解密即可获取页数的方法?

Is there a way to get the number of pages without decrypting?

推荐答案

您可以使用 pdfrw

示例

a.pdf b.pdf 是相同的pdf.区别是 b.pdf 受密码保护的pdf a.pdf 是简单的pdf 没有任何保护,并且没有页数是30

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

详细信息 PDFRW

For in detail PDFRW

这篇关于Python从受密码保护的pdf获取页面数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆