Encrypt and Decrypt PDF using PyPDF2 - GeeksforGeeks

01 Oct, 2020
PDF (Portable Document Format) is one of the most used file formats for storing and sending documents. They are commonly used for many purposes such as eBooks, Resumes, Scanned documents, etc. But as we share pdf to many people, there is a possibility of its data getting leaked or stolen. So, it’s necessary to password protect our PDF files so that only authorized persons can have access to it.
In this article, we are going to see how can we set a password to protect a PDF file. We’ll be using the PyPDF2 module to encrypt and decrypt our PDF files. PyPDF2 is a Python library built as a PDF toolkit. It is capable of:
Extracting document information (title, author, ...)
Splitting and Merging documents
Cropping pages
Encrypting and decrypting PDF files
PyPDF2 is not an inbuilt library, so we have to install it.
pip3 install PyPDF2

Now, we are ready to write our script to encrypt PDF files.
First, We will open our PDF file with the reader object. Then, we will create a copy of the original file so that if something goes wrong, it doesn’t affect our original file. To create a copy, we have to iterate through every page of the file and add it to our new PDF file. Then, we can simply encrypt our new PDF file.
PDF File used:
Python3
# import PdfFileWriter and PdfFileReader # class from PyPDF2 libraryfrom PyPDF2 import PdfFileWriter, PdfFileReader  # create a PdfFileWriter objectout = PdfFileWriter()  # Open our PDF file with the PdfFileReaderfile = PdfFileReader("myfile.pdf")  # Get number of pages in original filenum = file.numPages  # Iterate through every page of the original # file and add it to our new file.for idx in range(num):        # Get the page at index idx    page = file.getPage(idx)          # Add it to the output file    out.addPage(page)    # Create a variable password and store # our password in it.password = "pass"  # Encrypt the new file with the entered passwordout.encrypt(password)  # Open a new file "myfile_encrypted.pdf"with open("myfile_encrypted.pdf", "wb") as f:        # Write our encrypted PDF to this file    out.write(f)
Output:
This will create a copy of the original file and encrypt it with the entered password. Once the PDF is encrypted, it can not be opened without entering the correct password.
But what if we want to decrypt the encrypted PDF file? We can do this too with this library. The process is almost the same. We will open the encrypted file with the correct password and create a copy of it by iterating through every page of it and adding it to our new PDF file.
Here’s the code:
Python3
# import PdfFileWriter and PdfFileReader # class from PyPDF2 libraryfrom PyPDF2 import PdfFileWriter, PdfFileReader  # Create a PdfFileWriter objectout = PdfFileWriter()  # Open encrypted PDF file with the PdfFileReaderfile = PdfFileReader("myfile_encrypted.pdf")  # Store correct password in a variable password.password = "pass"  # Check if the opened file is actually Encryptedif file.isEncrypted:      # If encrypted, decrypt it with the password    file.decrypt(password)      # Now, the file has been unlocked.    # Iterate through every page of the file    # and add it to our new file.    for idx in range(file.numPages):                # Get the page at index idx        page = file.getPage(idx)                  # Add it to the output file        out.addPage(page)          # Open a new file "myfile_decrypted.pdf"    with open("myfile_decrypted.pdf", "wb") as f:                # Write our decrypted PDF to this file        out.write(f)      # Print success message when Done    print("File decrypted Successfully.")else:        # If file is not encrypted, print the     # message    print("File already decrypted.")
This will create a copy of the encrypted file that doesn’t require a password to be opened.
So, this was a basic script to encrypt and decrypt PDF files. But there are a plethora of ideas to extend this. You can create a GUI tool to do it or Develop a Web application that encrypts PDF files. You can also create a whole PDF Manager using the PyPDF2 library.
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()