Skip to content

Instantly share code, notes, and snippets.

@tieubinhco
Last active May 9, 2024 18:02
Show Gist options
  • Save tieubinhco/5654da0e412adffec07e73f4f6d04c3b to your computer and use it in GitHub Desktop.
Save tieubinhco/5654da0e412adffec07e73f4f6d04c3b to your computer and use it in GitHub Desktop.
Decrypt PDF file by Python pyPDF
#For python >= 3.9
#Required to install pypdf by pip install pypdf
#There will be an output "Multiple definitions in dictionary at byte 0x1dc for key /Filter", but it's ok, file will still be decrypted
from pypdf import PdfReader, PdfWriter
def decrypt_pdf(input_path, output_path, password):
with open(input_path, 'rb') as input_file, open(output_path, 'wb') as output_file:
reader = PdfReader(input_file,strict=False)
reader.decrypt(password)
writer = PdfWriter()
for i in range(len(reader.pages)):
writer.add_page(reader.pages[i])
writer.write(output_file)
if __name__ == '__main__':
# example usage:
input_file="C:/Testfolder/Test_encrypted_file.pdf"
output_file="C:/Testfolder/Test_decrypted_file.pdf"
decrypt_password="12345abcde"
decrypt_pdf(input_file,output_file ,decrypt_password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment