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

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/85269745

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

猜你喜欢

转载自blog.csdn.net/mouday/article/details/85269745