您当前的位置: 首页 >  彭世瑜 Python

Python编程:pypdf2和pdfplumber获取pdf文件的页数

彭世瑜 发布时间:2018-12-26 22:10:48 ,浏览量:3

pypdf2

安装

pip install pypdf2

代码实例

from PyPDF2 import PdfFileReader

filename = "test.pdf"
reader = PdfFileReader(filename)

# 不解密可能会报错:PyPDF2.utils.PdfReadError: File has not been decrypted
if reader.isEncrypted:
    reader.decrypt('')

page = reader.getNumPages()
print(page)

"""
如果加密是高版本的(3, 4),可能会报错
NotImplementedError: only algorithm code 1 and 2 are supported

原因是:
代码中有版本判断
if not (encrypt['/V'] in (1, 2)):
    raise NotImplementedError("only algorithm code 1 and 2 are supported")
"""

参考: https://github.com/mstamy2/PyPDF2/issues/51#issuecomment-437839902

pdfplumber

安装

pip install pdfplumber

代码示例

import pdfplumber

filename = "test.pdf"
f = pdfplumber.open(filename)
print(len(f.pages))

就是那么简单,没有过多的繁琐操作,暂时没有发现其他莫名问题

实际使用中发现前者速度要快很多

参考 https://github.com/jsvine/pdfplumber

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 3浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0563s