【问题标题】:Open a protected pdf file in python在python中打开一个受保护的pdf文件
【发布时间】:2014-11-25 14:59:31
【问题描述】:

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

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

【问题讨论】:

    标签: python pdf passwords


    【解决方案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')
    

    【讨论】:

    • 这里的decrypted.pdf是什么?
    • 那是output_path,要写入的out文件名。
    • 只想说我必须删除密码参数和reader.decrypt(password) 部分才能使其工作。
    • 这是怎么回事?你的 PDF 真的加密了吗?这是脚本的要点。
    • 我在reader.decrypt(password) 行收到错误消息。 only algorithm code 1 and 2 are supported。我认为这与我的 pdf 的加密方式有关。
    【解决方案2】:

    您现在应该改用pikepdf 库:

    import pikepdf
    
    with pikepdf.open("input.pdf", password="abc") as pdf:
        num_pages = len(pdf.pages)
        print("Total pages:", num_pages)
    

    PyPDF2 不支持很多加密算法,pikepdf 似乎解决了它们,它支持大多数密码保护的方法,并且有文档并积极维​​护。

    【讨论】:

    【解决方案3】:

    我有这个问题的答案。基本上,需要安装和使用 PyPDF2 库才能使这个想法发挥作用。

    #When you have the password = abc you have to call the function decrypt in PyPDF to decrypt the pdf file
    filePath = raw_input("Enter pdf file path: ")
    f = PdfFileReader(file(filePath, "rb"))
    output = PdfFileWriter()
    f.decrypt ('abc')
    
    # Copy the pages in the encrypted pdf to unencrypted pdf with name noPassPDF.pdf
    for pageNumber in range (0, f.getNumPages()):
       output.addPage(f.getPage(pageNumber))
       # write "output" to noPassPDF.pdf
       outputStream = file("noPassPDF.pdf", "wb")
       output.write(outputStream)
       outputStream.close()
    
    #Open the file now
       if sys.platform.startswith('darwin'):#open in MAC OX
           subprocess.call(["open", "noPassPDF.pdf"])
    

    【讨论】:

    • "raise NotImplementedError("仅支持算法代码 1 和 2")" 错误。
    【解决方案4】:

    您可以使用 pdfplumber 库。超级好用,可以无缝读取机器编写的 pdf 文件,比我用过的任何其他库都好。

    import pdfplumber
    with pdfplumber.open(r'D:\examplepdf.pdf' , password = 'abc') as pdf:
        first_page = pdf.pages[0]
        print(first_page.extract_text())
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 2021-03-28
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多