Decrypt a pdf file with python

def decrypt_pdf(input_path, output_path, password):
    try:
      with open(input_path, 'rb') as input_file, \
        open(output_path, 'wb') as output_file:
        reader = PdfFileReader(input_file, strict=False)
        reader.decrypt(password)
        writer = PdfFileWriter()
        for i in range(reader.getNumPages()):
          writer.addPage(reader.getPage(i))

        writer.write(output_file)
    except NotImplementedError:
        from shutil import copyfile
        copyfile(input_path, "temp.pdf")
        command="qpdf --password=\"" + password + "\" --decrypt temp.pdf "+output_path
        os.system(command)
        if os.path.isfile('temp.pdf'):
            os.remove('temp.pdf')
    except KeyError:
        print ("Decryption failed with an exception for " + input_path)

The second method of using qpdf is for the chance that decryption fails in some cases. In this cases, you need qpdf to be installed:

sudo apt install qpdf