Python 3 Script to Encrypt and Decrypt PDF Documents With Password Using PyPDF2 Library Full Project For Beginners

Python 3 Script to Encrypt and Decrypt PDF Documents With Password Using PyPDF2 Library Full Project For Beginners

 

 

 

Welcome folks today in this post we will be encrypting and decrypting  pdf documents with password in python 3 using pypdf2 library. All the source code of the project is given below.

 

 

Get Started

 

 

In order to get started you need to install pypdf2 library using pip command as follows

 

pip install pypdf2

 

 

After installing it you need to create a app.py file and copy paste the following code

 

 

app.py

 

 

# import PdfFileWriter and PdfFileReader 
# class from PyPDF2 library 
from PyPDF2 import PdfFileWriter, PdfFileReader 

# create a PdfFileWriter object 
out = PdfFileWriter() 

# Open our PDF file with the PdfFileReader 
file = PdfFileReader("fancy.pdf") 

# Get number of pages in original file 
num = 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 password 
out.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)

 

See also  Python 3 Script to Find the Largest Element in Array or List Using For Loop Full Tutorial For Beginners

 

 

Now in this python script you will see we are taking the input pdf file and we are encrypting it with a password and a output file is created.

 

 

 

 

 

As you can see if you open the pdf file then you need to enter the password which you configured at time of encryption

 

 

Decryption of PDF

 

 

Now comes the decryption part. When we want to decrypt the pdf using the password we know.

 

 

# import PdfFileWriter and PdfFileReader 
# class from PyPDF2 library 
from PyPDF2 import PdfFileWriter, PdfFileReader 

# Create a PdfFileWriter object 
out = PdfFileWriter() 

# Open encrypted PDF file with the PdfFileReader 
file = PdfFileReader("myfile_encrypted.pdf") 

# Store correct password in a variable password. 
password = "pass"

# Check if the opened file is actually Encrypted 
if 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.")

 

See also  Python 3 Binary Search Algorithm Script to Find Number Position (Recursive and Iterative) in Array Full Project For Beginners

 

 

Now you will see a decrypted_pdf file will be created in the root directory

 

 

 

 

Leave a Reply