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
Set up Port Forwarding for Pace 5031
True Riches In Christ
Creepshotorg
3 Tick Granite Osrs
Xre-02022
Chambersburg star athlete JJ Kelly makes his college decision, and he’s going DI
Nikki Catsouras Head Cut In Half
More Apt To Complain Crossword
Weather In Moon Township 10 Days
Prices Way Too High Crossword Clue
Watch TV shows online - JustWatch
Reddit Wisconsin Badgers Leaked
Alaska: Lockruf der Wildnis
Illinois Gun Shows 2022
iLuv Aud Click: Tragbarer Wi-Fi-Lautsprecher für Amazons Alexa - Portable Echo Alternative
What is Rumba and How to Dance the Rumba Basic — Duet Dance Studio Chicago | Ballroom Dance in Chicago
Urban Airship Expands its Mobile Platform to Transform Customer Communications
Obsidian Guard's Cutlass
Schedule 360 Albertsons
Concordia Apartment 34 Tarkov
Pecos Valley Sunland Park Menu
Aerocareusa Hmebillpay Com
1973 Coupe Comparo: HQ GTS 350 + XA Falcon GT + VH Charger E55 + Leyland Force 7V
Gotcha Rva 2022
Airtable Concatenate
Foodsmart Jonesboro Ar Weekly Ad
Idle Skilling Ascension
Snohomish Hairmasters
Lovindabooty
How do you get noble pursuit?
Maths Open Ref
Till The End Of The Moon Ep 13 Eng Sub
Best New England Boarding Schools
Springfield.craigslist
Mumu Player Pokemon Go
Word Trip Level 359
Sedano's Supermarkets Expands to Orlando - Sedano's Supermarkets
Ni Hao Kai Lan Rule 34
Seymour Johnson AFB | MilitaryINSTALLATIONS
Puffco Peak 3 Red Flashes
1v1.LOL Game [Unblocked] | Play Online
Fetus Munchers 1 & 2
Riverton Wyoming Craigslist
Home Auctions - Real Estate Auctions
Craigslist Food And Beverage Jobs Chicago
Craigslist/Nashville
Ucla Basketball Bruinzone
Craigslist St Helens
What is a lifetime maximum benefit? | healthinsurance.org
Wzzm Weather Forecast
Acuity Eye Group - La Quinta Photos
Access One Ummc
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.