Skip to content

Commit 3a8392a

Browse files
committed
add pdf locker tutorial
1 parent 64cd008 commit 3a8392a

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4949
- [How to Create a Zip File Locker in Python](https://thepythoncode.com/article/build-a-zip-file-locker-in-python). ([code](ethical-hacking/zip-file-locker))
5050
- [How to Implement the Caesar Cipher in Python](https://thepythoncode.com/article/implement-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
5151
- [How to Crack the Caesar Cipher in Python](https://thepythoncode.com/article/how-to-crack-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
52+
- [How to Lock PDFs in Python](https://thepythoncode.com/article/lock-pdfs-in-python). [(code)](ethical-hacking/pdf-locker)
5253

5354
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5455
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

ethical-hacking/pdf-locker/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Lock PDFs in Python](https://thepythoncode.com/article/lock-pdfs-in-python)
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Import the necessary libraries
2+
import PyPDF2, getpass # getpass is for getting password with some level of security
3+
from colorama import Fore, init
4+
5+
# Initialize colorama for colored output
6+
init()
7+
8+
9+
# Function to lock pdf
10+
def lock_pdf(input_file, password):
11+
with open(input_file, 'rb') as file:
12+
# Create a PDF reader object
13+
pdf_reader = PyPDF2.PdfReader(file)
14+
15+
# Create a PDF writer object
16+
pdf_writer = PyPDF2.PdfWriter()
17+
18+
# Add all pages to the writer
19+
for page_num in range(len(pdf_reader.pages)):
20+
pdf_writer.add_page(pdf_reader.pages[page_num])
21+
22+
# Encrypt the PDF with the provided password
23+
pdf_writer.encrypt(password)
24+
25+
# Write the encrypted content back to the original file
26+
with open(input_file, 'wb') as output_file:
27+
pdf_writer.write(output_file)
28+
29+
30+
# Get user input
31+
input_pdf = input("Enter the path to the PDF file: ")
32+
password = getpass.getpass("Enter the password to lock the PDF: ")
33+
34+
# Lock the PDF using PyPDF2
35+
print(f'{Fore.GREEN}[!] Please hold on for a few seconds..')
36+
lock_pdf(input_pdf, password)
37+
38+
# Let the user know it's done
39+
print(f"{Fore.GREEN}[+] PDF locked successfully.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PyPDF2
2+
colorama

0 commit comments

Comments
 (0)