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

Commit1bac150

Browse files
committed
Add ch 13 second challenge practice file and solution
1 parent2c6d799 commit1bac150

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 13.7 - Challenge: Unscramble a PDF
2+
# Solution to challenge
3+
4+
frompathlibimportPath
5+
6+
fromPyPDF2importPdfFileReader,PdfFileWriter
7+
8+
9+
defget_page_text(page):
10+
returnpage.extractText()
11+
12+
13+
pdf_path=Path.home()/"github/realpython/python-basics-exercises/" \
14+
"ch13-interact-with-pdf-files/practice_files/scrambled.pdf"
15+
16+
pdf_reader=PdfFileReader(str(pdf_path))
17+
pdf_writer=PdfFileWriter()
18+
19+
pages=list(pdf_reader.pages)
20+
pages.sort(key=get_page_text)
21+
22+
forpageinpages:
23+
rotation_degrees=page["/Rotate"]
24+
ifrotation_degrees<0:
25+
page.rotateClockwise(-rotation_degrees)
26+
elifrotation_degrees>0:
27+
page.rotateCounterClockwise(rotation_degrees)
28+
pdf_writer.addPage(page)
29+
30+
output_path=Path.home()/"unscrambled.pdf"
31+
withoutput_path.open(mode="wb")asoutput_file:
32+
pdf_writer.write(output_file)
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp