Skip to content

Commit 5eb1d56

Browse files
committed
Password Protection of PDF File
1 parent f550f55 commit 5eb1d56

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import PyPDF2
2+
import os
3+
4+
def protect_pdf(input_path, output_path, password):
5+
with open(input_path, 'rb') as file:
6+
pdf_reader = PyPDF2.PdfReader(file)
7+
pdf_writer = PyPDF2.PdfWriter()
8+
9+
for page_num in range(len(pdf_reader.pages)):
10+
pdf_writer.add_page(pdf_reader.pages[page_num])
11+
12+
pdf_writer.encrypt(password)
13+
14+
output_folder = os.path.dirname(output_path)
15+
os.makedirs(output_folder, exist_ok=True)
16+
17+
with open(output_path, 'wb') as output_file:
18+
pdf_writer.write(output_file)
19+
20+
if __name__ == "__main__":
21+
input_pdf_path = "pdf-file/logo.pdf"
22+
output_pdf_path = "output/protected_logo.pdf"
23+
password = "123456789"
24+
25+
protect_pdf(input_pdf_path, output_pdf_path, password)
26+
print(f"PDF file protected and saved to: {output_pdf_path}")

pdf-file/logo.pdf

174 KB
Binary file not shown.

0 commit comments

Comments
 (0)