Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a8392a

Browse files
committed
add pdf locker tutorial
1 parent64cd008 commit3a8392a

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 1 addition & 0 deletions
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)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Import the necessary libraries
2+
importPyPDF2,getpass# getpass is for getting password with some level of security
3+
fromcoloramaimportFore,init
4+
5+
# Initialize colorama for colored output
6+
init()
7+
8+
9+
# Function to lock pdf
10+
deflock_pdf(input_file,password):
11+
withopen(input_file,'rb')asfile:
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+
forpage_numinrange(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+
withopen(input_file,'wb')asoutput_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.")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PyPDF2
2+
colorama

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp