8000 Add ch13 section 5 exercise 1 solution · MHHamdan/python-basics-exercises@56b6289 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56b6289

Browse files
committed
Add ch13 section 5 exercise 1 solution
1 parent 3000f45 commit 56b6289

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 13.5 - Encrypting and Decrypting PDFs
2+
# Solutions to review exercises
3+
4+
# ***********
5+
# Exercise 1
6+
#
7+
# In the Chapter 13 Practice Files folder there is a PDF file called
8+
# `top_secret.pdf`. Encrypt the file with the user password
9+
# `Unguessable`. Save the encrypted file as in your home directory as
10+
# `top_secret_encrypted.pdf`.
11+
# ***********
12+
13+
from pathlib import Path
14+
15+
from PyPDF2 import PdfFileReader, PdfFileWriter
16+
17+
18+
pdf_path = Path.home() / "python-basics-exercises/" \
19+
"ch13-interact-with-pdf-files/practice_files/top_secret.pdf"
20+
21+
pdf_reader = PdfFileReader(str(pdf_path))
22+
pdf_writer = PdfFileWriter()
23+
24+
pdf_writer.appendPagesFromReader(pdf_reader)
25+
pdf_writer.encrypt(user_pwd="Unguessable")
26+
27+
output_path = Path.home() / "top_secret_encrypted.pdf"
28+
with output_path.open(mode="wb") as output_file:
29+
pdf_writer.write(output_file)

0 commit comments

Comments
 (0)
0