Skip to content

Commit ec2eec0

Browse files
committed
Add ch 13 PdfFileSplitter challenge solution
1 parent 5041815 commit ec2eec0

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+
from pathlib import Path
5+
6+
from PyPDF2 import PdfFileReader, PdfFileWriter
7+
8+
9+
class PdfFileSplitter:
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+
def split(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+
for page in self.pdf_reader.pages[:breakpoint]:
27+
self.writer1.addPage(page)
28+
# Add all the remaining pages to writer2
29+
for page in self.pdf_reader.pages[breakpoint:]:
30+
self.writer2.addPage(page)
31+
32+
def write(self, filename):
33+
"""Write both PdfFileWriter instances to files"""
34+
# Write the first file to <filename>_1.pdf
35+
with Path(str(filename) + "_1.pdf").open(mode="wb") as output_file:
36+
self.writer1.write(output_file)
37+
# Write the second file to <filename>_2.pdf
38+
with Path(str(filename) + "_2.pdf").open(mode="wb") as output_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.py renamed 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.py renamed 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.py renamed 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy