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

Commitec2eec0

Browse files
committed
Add ch 13 PdfFileSplitter challenge solution
1 parent5041815 commitec2eec0

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 13.5 - Challenge: PdfFileSplitter Class
2+
# Solution to challenge
3+
4+
frompathlibimportPath
5+
6+
fromPyPDF2importPdfFileReader,PdfFileWriter
7+
8+
9+
classPdfFileSplitter:
10+
"""Class for splitting a PDF into two files."""
11+
12+
def__init__(self,pdf_path):
13+
# Open the PDF file with a new PdfFileReader instance
14+
self.pdf_reader=PdfFileReader(str(pdf_path))
15+
# Initialize the .writer1 and .writer2 attributes to None
16+
self.writer1=None
17+
self.writer2=None
18+
19+
defsplit(self,breakpoint):
20+
"""Split the PDF into two PdfFileWriter instances"""
21+
# Set .writer1 and .writer2 to new PdfFileWriter intances
22+
self.writer1=PdfFileWriter()
23+
self.writer2=PdfFileWriter()
24+
# Add all pages up to, but not including, the breakpoint
25+
# to writer1
26+
forpageinself.pdf_reader.pages[:breakpoint]:
27+
self.writer1.addPage(page)
28+
# Add all the remaining pages to writer2
29+
forpageinself.pdf_reader.pages[breakpoint:]:
30+
self.writer2.addPage(page)
31+
32+
defwrite(self,filename):
33+
"""Write both PdfFileWriter instances to files"""
34+
# Write the first file to <filename>_1.pdf
35+
withPath(str(filename)+"_1.pdf").open(mode="wb")asoutput_file:
36+
self.writer1.write(output_file)
37+
# Write the second file to <filename>_2.pdf
38+
withPath(str(filename)+"_2.pdf").open(mode="wb")asoutput_file:
39+
self.writer2.write(output_file)
40+
41+
42+
pdf_splitter=PdfFileSplitter("ch13-interact-with-pdf-files/practice_files/Pride_and_Prejudice.pdf")
43+
pdf_splitter.split(breakpoint=150)
44+
pdf_splitter.write("pride_split")

‎ch13-interact-with-pdf-files/3-concatenating-and-merging-pdfs.pyrenamed to‎ch13-interact-with-pdf-files/4-concatenating-and-merging-pdfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 13.3 - Concatenating and Merging PDFs
1+
# 13.4 - Concatenating and Merging PDFs
22
# Solutions to review exercises
33

44
# ***********

‎ch13-interact-with-pdf-files/4-rotating-and-cropping-pdf-pages.pyrenamed to‎ch13-interact-with-pdf-files/5-rotating-and-cropping-pdf-pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 13.4 - Rotating and Cropping PDF pages
1+
# 13.5 - Rotating and Cropping PDF pages
22
# Solutions to review exercises
33

44
# ***********

‎ch13-interact-with-pdf-files/5-encrypting-and-decrypting-pdfs.pyrenamed to‎ch13-interact-with-pdf-files/6-encrypting-and-decrypting-pdfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 13.5 - Encrypting and Decrypting PDFs
1+
# 13.6 - Encrypting and Decrypting PDFs
22
# Solutions to review exercises
33

44
# ***********
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp