在python中读取密码保护的pdf?

时间:2018-06-08 12:28:59

标签: python python-3.x pypdf2

我正在尝试阅读password protected pdf,我有password

我尝试了以下

from PyPDF2 import PdfFileReader, PdfFileWriter

input_path = r'D:\report.pdf'
output_path = r'D:\report'
output_file = 'report_test.pdf'
passowrd = 'REDACTED'




def decrypt_pdf(input_path, output_path, password):
    with open(input_path, 'rb') as input_file, \
    open(output_path, 'wb') as output_file:
    reader = PdfFileReader(input_file)
    reader.decrypt(password)

    writer = PdfFileWriter()

    for i in range(reader.getNumPages()):
      writer.addPage(reader.getPage(i))

    writer.write(output_file)

我收到以下错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 10, in decrypt_pdf
  File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 1987, in decrypt
    return self._decrypt(password)
  File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 1997, in _decrypt
    user_password, key = self._authenticateUserPassword(password)
  File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2039, in >_authenticateUserPassword
    encrypt.get("/EncryptMetadata", BooleanObject(False)).getObject())
  File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2973, in _alg35
    key = _alg32(password, rev, keylen, owner_entry, p_entry, id1_entry)
  File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2876, in _alg32
    p_entry = struct.pack('<i', p_entry)
struct.error: argument out of range

我应该使用不同的模块吗?我查了qpdf,但pip不会安装它。

0 个答案:

没有答案