【问题标题】:How do I remove passwords of some of all the pdf files in a directory with python?如何使用python删除目录中所有pdf文件的密码?
【发布时间】:2020-12-20 12:04:01
【问题描述】:

我的目录中有一些 pdf 文件。其中有些受密码保护,有些则不受密码保护。我知道每个受密码保护的文件的密码。如何自动化从每个 pdf 文件中删除密码的过程?我正在考虑类似的事情:

  1. 获取密码保护文件。
  2. 从我制作的单词表中尝试给定的密码。
  3. 打印出文件的密码。
  4. 将文件另存为“Decrypted_filename.pdf

【问题讨论】:

    标签: python python-3.x pdf unix


    【解决方案1】:

    我找到了一个有用的库,pikepdf,它基于 qpdf 并自动将 pdf 转换为可提取的。

    使用它的代码非常简单:

    import pikepdf
    
    pdf = pikepdf.open('unextractable.pdf')
    pdf.save('extractable.pdf')
    

    如要删除密码,可以设置密码

    pikepdf.open('unextractable.pdf', password='thepassword')
    

    【讨论】:

    • pdf = pikepdf.open(path, password='xxxxxxxxx', allow_overwriting_input=True) 如果您希望从受密码保护的 pdf 中删除密码,则此方法有效
    【解决方案2】:

    我认为你可以用 pyPdf 解决你的问题

    【讨论】:

      【解决方案3】:

      试试这个:

      import pikepdf
      paths=[...] # path of all files
      passwords=[...] # passwords 
      outputPaths=[...] # output file path
      for i in range(len(paths)):
          pdf = pikepdf.open(paths[i],password=passwords[i])
          pdf.save(outputPaths[i])
      

      注意:

      pip 安装 pikepdf
      如果文件没有密码保护,则使用:- pdf = pikepdf.open('NAME')

      【讨论】:

        【解决方案4】:

        想通了!
        这是我的代码:

        import pikepdf
        import os
        import shutil
        
        # Load Wordlist
        wordlist = input('Enter wordlist: ')
        passwords = passwords = [ line.strip() for line in open(wordlist) ]
        
        # Making Directory
        try:
            os.mkdir('Decrypted')
        except FileExistsError as e:
            pass
        
        # iterate over passwords
        def crack(p):
         if ".pdf" in str(p):
          #print('\nOpening: '+str(p))
          print(str(p))
          for password in passwords:
           try:
            # open PDF file
            with pikepdf.open(p, password=password) as pdf:
             # Password decrypted successfully, break out of the loop
             print("[+] Password found:", password)
             x=i.split('.pdf')[0]
             y=x+"_DECRYPTED.pdf"
             pdf.save(y)
             shutil.move(y, 'Decrypted')
             pdf.close()
             #os.remove(i)
             break
           except pikepdf._qpdf.PasswordError as e:
            # wrong password, just continue in the loop
            continue
         else:
          pass
        
        for i in os.listdir(os.getcwd()):
         crack(i)
        

        【讨论】:

          猜你喜欢
          • 2011-01-01
          • 2021-11-10
          • 2022-08-09
          • 2021-12-01
          • 2017-05-13
          • 2014-06-03
          • 2015-01-20
          • 1970-01-01
          • 2014-10-27
          相关资源
          最近更新 更多