How to Encrypt a PDF Using Python | HackerNoon (2024)

Encryption is a way of encoding human-readable data with encrypted code that enables the use of a password to view the source and data stored.

One use case that applies to encrypting data with a portable document format (PDF) is when your bank sends your statement of account. To access the document, you need a password to view the document. This way, you are prompted to enter a password which makes it difficult for someone else to access, even if they have access to your computer and email.

This article will teach you how to write a Python script that helps you password-protect your PDF before sharing.

Getting Started

To build the PDF encryption project, you must install the package, PyPDF2.

PyPDF2: This free and open-source Python library is capable of splitting, merging, and processing PDFs, cropping, rotating, and transforming a file to your desired specification. With this package's installation, this tutorial's focus will be adding passwords to PDF files.

Run this command to install the library:

pip install PyPDF2

To confirm the package installation, run this command:

pip list

Creating the PDF Encryption

Before writing any scripts, let’s create a new folder that will house a Python file and a PDF of your choice saved in this folder. Make sure to have the PDF from your local machine.

The directory structure should look like this in the folder pdf:

How to Encrypt a PDF Using Python | HackerNoon (1)How to Encrypt a PDF Using Python | HackerNoon (2)In the new file, pdf_encryption.py, copy and paste the following code:

# pdf_encryption.pyfrom PyPDF2 import PdfReader, PdfWriterreader = PdfReader("WBW3.pdf")writer = PdfWriter()for page in reader.pages: writer.add_page(page)writer.encrypt("YOUR-PASSWORD-HERE")

The code block above does the following:

  • Import the PdfReader and PdfWriter functions from the module PyPDF2
  • The reader variable reads the PDF and initializes it as an object
  • The PdfWriter() class supports writing the PDF files out that will later make sure the file is saved
  • Next, using the for loop, iterate through the PDF pages and add all the pages to the writer
  • Add a password to the new PDF. Choose a more secured password

Saving the New PDF

Copy and update the pdf_encryption.py file:

# pdf_encryption.pyfrom PyPDF2 import PdfReader, PdfWriterreader = PdfReader("WBW3.pdf")writer = PdfWriter()for page in reader.pages: writer.add_page(page)writer.encrypt("YOUR-PASSWORD-HERE")# add thiswith open("blockchain.pdf", "wb") as f: writer.write(f)

The with open command is used to open a file with a document's name, renamed as f. The wb keyword is meant to open the file for writing in the binary format and overwrites the existing file if it already exists; otherwise, it creates a new file which we stated as blockchain.pdf.

How to Encrypt a PDF Using Python | HackerNoon (3)How to Encrypt a PDF Using Python | HackerNoon (4)

Conclusion

Encrypting sensitive data is something to consider when sending documents over the internet.

But in this scenario, you don’t have to write another script to be able to open the document, and it can be done by sharing the secret password for another user to gain access.

This article taught you how to encode and make PDFs more secure with the option to password-protect them for privacy purposes.

Learn More

How to Encrypt a PDF Using Python | HackerNoon (2024)
Top Articles
How Many People Really Save $1 Million for Retirement?
The Comeback of Axie Infinity
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 5616

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.