Skip to content

Commit 6ccaa87

Browse files
committed
Add ch13 section 4 exercise 1 solution
1 parent 65559d8 commit 6ccaa87

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# 13.4 - Rotating and Cropping PDF pages
22
# Solutions to review exercises
3+
4+
# ***********
5+
# Exercise 1
6+
#
7+
# In the Chapter 13 Practice Files folder there is a PDF called
8+
# `split_and_rotate.pdf`. Create a new PDF called `rotated.pdf` in your
9+
# home directory containing the pages of `split_and_rotate.pdf` rotated
10+
# counter-clockwise 90 degrees.
11+
# ***********
12+
13+
from pathlib import Path
14+
15+
from PyPDF2 import PdfFileReader, PdfFileWriter
16+
17+
18+
pdf_path = Path.home() / "github/realpython/python-basics-exercises/" \
19+
"ch13-interact-with-pdf-files/practice_files/split_and_rotate.pdf"
20+
21+
pdf_reader = PdfFileReader(str(pdf_path))
22+
pdf_writer = PdfFileWriter()
23+
24+
for page in pdf_reader.pages:
25+
rotated_page = page.rotateCounterClockwise(90)
26+
pdf_writer.addPage(rotated_page)
27+
28+
output_path = Path.home() / "rotated.pdf"
29+
with output_path.open(mode="wb") as output_file:
30+
pdf_writer.write(output_file)

0 commit comments

Comments
 (0)