在python中打开受保护的pdf文件

新手上路,请多包涵

我写了一个pdf破解,找到了受保护pdf文件的密码。我想用 Python 编写一个程序,无需密码即可在屏幕上显示该 pdf 文件。我使用 PyPDF 库。我知道如何在没有密码的情况下打开文件,但无法弄清楚受保护的文件。知道吗?谢谢

filePath = raw_input()
password = 'abc'
if sys.platform.startswith('linux'):
       subprocess.call(["xdg-open", filePath])

原文由 KL84 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 473
1 个回答

KL84 显示的方法基本有效,但代码不正确(它为每一页写入输出文件)。清理后的版本在这里:

https://gist.github.com/bzamecnik/1abb64affb21322256f1c4ebbb59a364

 # Decrypt password-protected PDF in Python.
#
# Requirements:
# pip install PyPDF2

from PyPDF2 import PdfFileReader, PdfFileWriter

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)

if __name__ == '__main__':
  # example usage:
  decrypt_pdf('encrypted.pdf', 'decrypted.pdf', 'secret_password')

原文由 Bohumir Zamecnik 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题