Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/ce120d6c88c2cdc52cab80a24d227c91 to your computer and use it in GitHub Desktop.

Select an option

Save aont/ce120d6c88c2cdc52cab80a24d227c91 to your computer and use it in GitHub Desktop.

PDF Page Splitter with Configurable Grid Using pypdf

  1. Configurable Grid Splitting via CLI Options

    • Uses argparse to accept an input PDF path, output PDF path, and optional --cols and --rows arguments, allowing the user to define how many horizontal and vertical splits to apply (default: 2 columns × 4 rows).
  2. Precise Page Cropping Based on Page Dimensions

    • Reads the first page of the input PDF, calculates the page width and height, and derives each tile’s size by dividing these dimensions by the specified number of columns and rows. It then computes crop coordinates for each grid cell.
  3. Tile Generation with Safe Page Copying

    • For each grid cell, creates a copy of the original page, adjusts its cropbox to the corresponding region, and adds it to a PdfWriter. Finally, all cropped tiles are written as separate pages into the output PDF file.
import argparse
from pypdf import PdfReader, PdfWriter
def split_pdf(input_path: str, output_path: str, num_cols: int, num_rows: int) -> None:
reader = PdfReader(input_path)
page = reader.pages[0]
page_width = float(page.mediabox.width)
page_height = float(page.mediabox.height)
crop_width = page_width / num_cols
crop_height = page_height / num_rows
writer = PdfWriter()
# ix: horizontal index, iy: vertical index
for ix in range(num_cols):
for iy in reversed(range(num_rows)):
lower_left = (crop_width * ix, crop_height * iy)
upper_right = (crop_width * (ix + 1), crop_height * (iy + 1))
# Copy the original page before cropping to avoid affecting other tiles
page_copy = page.copy()
page_copy.cropbox.lower_left = lower_left
page_copy.cropbox.upper_right = upper_right
writer.add_page(page_copy)
with open(output_path, "wb") as f:
writer.write(f)
def main():
parser = argparse.ArgumentParser(
description="Split the first page of a PDF into a grid and create a new PDF."
)
parser.add_argument("input_pdf", help="Path to the input PDF file")
parser.add_argument("output_pdf", help="Path to the output PDF file")
parser.add_argument(
"--cols",
type=int,
default=2,
help="Number of horizontal splits (default: 2)"
)
parser.add_argument(
"--rows",
type=int,
default=4,
help="Number of vertical splits (default: 4)"
)
args = parser.parse_args()
if args.cols <= 0 or args.rows <= 0:
raise ValueError("--cols and --rows must be integers greater than or equal to 1.")
split_pdf(args.input_pdf, args.output_pdf, args.cols, args.rows)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment