Created
September 3, 2020 14:55
-
-
Save akarsh1995/d8a4aa412345acec71ab5191b32773c7 to your computer and use it in GitHub Desktop.
Encrypt your pdf files in bulk.
This file contains hidden or 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
| import PyPDF2 | |
| def encrypt_pdf(file_to_encrypt, password, save_file_as): | |
| pdf_writer = PyPDF2.PdfFileWriter() | |
| with open(file_to_encrypt, "rb") as pdf_input_file: | |
| pdf_reader = PyPDF2.PdfFileReader(pdf_input_file) | |
| for page_num in range(pdf_reader.numPages): | |
| page = pdf_reader.getPage(page_num) | |
| pdf_writer.addPage(page) | |
| pdf_writer.encrypt(password, use_128bit=True) | |
| with open(save_file_as, "wb") as result_pdf: | |
| pdf_writer.write(result_pdf) | |
| print( | |
| "Successfully encrypted pdf file!!", | |
| "saved as", save_file_as | |
| ) | |
| if __name__ == "__main__": | |
| file_to_encrypt = "encrypt_me.pdf" | |
| password = "qwerty123" | |
| save_file_as = "encrypted.pdf" | |
| encrypt_pdf(file_to_encrypt, password, save_file_as) |
This file contains hidden or 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
| PyPDF2==1.26.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment