Skip to content

Instantly share code, notes, and snippets.

@oyiptong
Created November 5, 2024 04:56
Show Gist options
  • Save oyiptong/19204dc07043ca4f0071e603ea3fa48b to your computer and use it in GitHub Desktop.
Save oyiptong/19204dc07043ca4f0071e603ea3fa48b to your computer and use it in GitHub Desktop.
A program that splits a PDF document into multiple 15-page documents.
import sys
from PyPDF2 import PdfWriter, PdfReader
inputpdf = PdfReader(open("book_name.pdf", "rb"))
pages = list(range(0, len(inputpdf.pages)))
chunk_size = 15
page_chunks = [pages[i:i + chunk_size] for i in range(0, len(inputpdf.pages), chunk_size)]
chunk_count = 0
for chunk in page_chunks:
chunk_count += 1
output = PdfWriter()
for page in chunk:
output.add_page(inputpdf.pages[page])
with open("document-chunk-{:02d}.pdf".format(chunk_count), "wb") as outputStream:
output.write(outputStream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment