DEV Community

Cover image for Encrypting And Decrypting PDF Files Using Python...
Naftal Rainer
Naftal Rainer

Posted on

Encrypting And Decrypting PDF Files Using Python...

Portable Document Format (PDF) is one of the most widely used file formats by online accessories.
Most of the documents e.g. eBooks, scanned documents e.t.c uses this pdf format for storage and sharing purposes.

For the purpose of sharing in various platforms and media, a document is exposed to so many threats such as information leakage and access by unauthorized individuals. It is therefore necessary for one to encrypt his/her documents to prevent such avoidable occurrences and exposure.

Python being a resourceful programming language offers a very awesome module known as PyPDF2 to encrypt and decrypt pdf files hence hence enhancing security of contained information.

The PyPDF2 library is capable of:

  1. Extracting document information e.g. title, page number e.t.c
  2. Splitting and merging documents.
  3. Cropping Pages.
  4. Encryption and decryption.

PyPDF2 is not an in-built library therefore it needs to be installed before use by:

pip3 install PyPDF2
Enter fullscreen mode Exit fullscreen mode

The files used in the code implementations below are found here
Encrypting a PDF file
File encryption entails addition of a password to a document thereby allowing access to only authorized people.

To do this, we'll follow the following steps:
Step 1: Open the pdf using the reader object.
Step 2: Create a copy of the original file by iterating through
every page and adding it to the new pdf file.
Step 3: Encrypt the new pdf file.

The Code Implementation

# Import the required module and sub-modules
from PyPDF2 import PdfFileWriter
from PyPDF2 import PdfFileReader

# Create a PdfFileWriter object
result = PdfFileWriter()

# Open the pdf file to encrypt
file = PdfFileReader('Magazine.pdf')

# Retrieve the number of pages to iterate in the original document
length = file.numPages

# Iterates through every page and adds it to the new file (a copy of the original)
for i in range(length):
    pages = file.getPage(i)
    result.addPage(pages)

# Creates a variable password.
password = 'pam&Lab890'

# Encrypt the file using the created password 
result.encrypt(password)

# Open a new file 'Magazines.pdf' and write the encrypted pdf file
with open('Magazines.pdf','wb') as f:
    result.write(f)

Enter fullscreen mode Exit fullscreen mode

This creates a similar copy of the original file (may have a different name as specified) that requires a set password to enable access.

Decrypting a PDF file
Decryption unlocks a document with a given password and converts it to a pdf file that does not require a password key to access.

The file decryption steps are almost analogous to that of encryption.

Step 1: Open the pdf using the reader object.
Step 2: Encrypt the new pdf file.
Step 3: Create a copy of the original file by iterating through

every page and adding it to the new pdf file.

The Code Implementation

# Import the required module and sub-modules
from PyPDF2 import PdfFileWriter
from PyPDF2 import PdfFileReader

# Create a PdfFileWriter object
result = PdfFileWriter()

# Open the password - secured pdf file to decrypt
file = PdfFileReader('Magazines.pdf')

# Creates a variable password.  
password = 'pam&Lab890'

# First, check if the file is encrypted then proceed if encrypted
if file.isEncrypted:

    # Decrypt the file using the givenpassword 
    file.decrypt(password)

     # Iterates through every page and adds it to the new file    
    for i in range(31):
        pages = file.getPage(i)
        result.addPage(pages)

    # Open a new file 'Magazines1.pdf' and write the encrypted 
        pdf file
    with open('Magazines1.pdf','wb') as f:
        result.write(f)

    print('File decrypted successfully')

else:

    print('File is not encrypted')

Enter fullscreen mode Exit fullscreen mode

If you guys found this post helpful then save it so you can refer anytime 😊. For those who wanna learn more about PyPDF2 module and functionalities, just give a recommendation of the specific area (whether merging, splitting e.t.c) in the comment section below.
Happy Coding Week! πŸ‘¨β€πŸ’» πŸ‘©β€πŸ’»

Top comments (0)