- Notifications
You must be signed in to change notification settings - Fork533
Update 3-challenge-PdfFileSplitter-class.py#87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Ranjit9632 wants to merge1 commit intorealpython:masterChoose a base branch fromRanjit9632:patch-4
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
85 changes: 42 additions & 43 deletionsch14-interact-with-pdf-files/3-challenge-PdfFileSplitter-class.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,46 @@ | ||
# the below code will work for python > 3.5 | ||
from pathlib import Path | ||
# the imported objects are as per the latest PyPDF2 | ||
from PyPDF2 importPdfReader, PdfWriter | ||
class PdfFileSplitter: | ||
# class instantiated with path string | ||
# pdf reader class cannot be used from inside the class as it is not defined only in init | ||
def __init__(self,pdf_path): | ||
self.pdf_path = pdf_path | ||
def split(self, superpoint, breakpoint): | ||
# instantiate the writerobject | ||
self.writer1 = PdfWriter() | ||
self.writer2 = PdfWriter() | ||
# break the pages till breakpoint | ||
for page in range(breakpoint): | ||
pages1 = self.pdf_path.pages[page] | ||
self.writer1.add_page(pages1) | ||
# get the last page as input and remember order is positional argument and then key argument | ||
for page in range(breakpoint, superpoint): | ||
pages1 = self.pdf_path.pages[page] | ||
self.writer2.add_page(pages1) | ||
def write(self): | ||
# save the files with 1 and 2 names then check it with print statement | ||
with Path("1.pdf").open(mode = "wb") as file: | ||
self.writer1.write(file) | ||
print(f"File 1.pdf created successfully.") | ||
with Path("2.pdf").open(mode = "wb") as file: | ||
self.writer2.write(file) | ||
pdf_og = Path.home()/"Python_Complete_Guide.pdf" | ||
print(pdf_og) | ||
# pdf reader instance | ||
input_pdf = PdfReader(str(pdf_og)) | ||
endpage = len(input_pdf.pages) | ||
print(endpage) | ||
# class is called with the instance created | ||
pf_splitter = PdfFileSplitter(input_pdf) | ||
pf_splitter.split(endpage,breakpoint = 150) | ||
pf_splitter.write() |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.