如何在 python 中使用 PyPDF2 阅读此 pdf 表单

How to read this pdf form using PyPDF2 in python

https://www.fda.gov/downloads/AboutFDA/ReportsManualsForms/Forms/UCM074728.pdf

我正在尝试使用 PyPDF2 或 Pdfminer 阅读此 pdf,但它说该文件尚未在 Pypdf2 和 pdfminer 中解密,它说它可以解压缩该 pdf。有人让我知道如何在 python3 windows 环境中执行此操作。我无法使用 poppler,因为我无法在此 windows.

中安装 poppler

这是受限制的 PDF 文件。在大多数情况下,您可以使用带有空字符串的 PyPDF2 解密不提示您输入密码的文件:

from PyPDF2 import PdfFileReader

reader = PdfFileReader('sample.pdf')
reader.decrypt('')

不幸的是,PyPDF2 decrypt() 方法不支持您的文件或任何其他具有 128-bit AES 加密级别的文件,这将 return NotImplementedError .

作为一种简单的解决方法,您可以将此文件另存为 Adob​​e Reader 或类似文件中的新文件,新文件应该适用于您的代码。

此外,您可以使用 qpdf 以编程方式完成此操作,如 this GitHub issue 中所述:

import os, shutil, tempdir
from subprocess import check_call

    try:
        tempdir = tempfile.mkdtemp(dir=os.path.dirname(filename))
        temp_out = os.path.join(tempdir, 'qpdf_out.pdf')
        check_call(['qpdf', "--password=", '--decrypt', filename, temp_out])
        shutil.move(temp_out, filename)
        print 'File Decrypted'

    finally:
        shutil.rmtree(tempdir)