-
-
Save nanda-dash/1a8e862ec2b9433f710314bf27ebd4b4 to your computer and use it in GitHub Desktop.
Decrypt password-protected PDF in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Decrypt password-protected PDF in Python. | |
# cleaned-up version of http://stackoverflow.com/a/26537710/329263 | |
# | |
# Requirements: | |
# pip install PyPDF2 | |
# | |
# Usage: decrypt_pdf('encrypted.pdf', 'decrypted.pdf', 'secret_password') | |
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment