paint-brush
How to Encrypt a PDF Using Pythonby@terieyenike
8,885 reads
8,885 reads

How to Encrypt a PDF Using Python

by TeriSeptember 6th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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. This article will teach you how to write a Python script that helps you password-protect your PDF before sharing. To build the PDF encryption project, you must install the package PyPDF2 This free, open-source Python library is capable of splitting, merging, and processing PDFs, cropping, rotating, and transforming a file to your desired specification.
featured image - How to Encrypt a PDF Using Python
Teri HackerNoon profile picture

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:


In the new file, pdf_encryption.py, copy and paste the following code:

# pdf_encryption.py

from PyPDF2 import PdfReader, PdfWriter
reader = 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.py

from PyPDF2 import PdfReader, PdfWriter
reader = PdfReader("WBW3.pdf")

writer = PdfWriter()

for page in reader.pages:
    writer.add_page(page)

writer.encrypt("YOUR-PASSWORD-HERE")

# add this
with 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.




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