PyPDF 2 Decrypt Not Working

前端 未结 7 1263
悲哀的现实
悲哀的现实 2020-12-15 20:38

Currently I am using the PyPDF 2 as a dependency.

I have encountered some encrypted files and handled them as you normally would (in the following code):

<         


        
7条回答
  •  清酒与你
    2020-12-15 21:01

    The following code could solve this problem:

    import os
    import PyPDF2
    from PyPDF2 import PdfFileReader
    
    fp = open(filename)
    pdfFile = PdfFileReader(fp)
    if pdfFile.isEncrypted:
        try:
            pdfFile.decrypt('')
            print('File Decrypted (PyPDF2)')
        except:
            command = ("cp "+ filename +
                " temp.pdf; qpdf --password='' --decrypt temp.pdf " + filename
                + "; rm temp.pdf")
            os.system(command)
            print('File Decrypted (qpdf)')
            fp = open(filename)
            pdfFile = PdfFileReader(fp)
    else:
        print('File Not Encrypted')
    

提交回复
热议问题