Managing and manipulating PDF files is a common task in document processing. Sometimes, you may need to divide each page of a PDF into two halves—such as separating the left and right sides of scanned book pages. The following Python script provides a simple and efficient way to accomplish this using the pypdf library.
The script, named split_pdf_halves.py, reads a PDF file and outputs a new one where each page is split into two separate pages: the left half and the right half. It handles both portrait and landscape orientations, as well as page rotation information, ensuring that the resulting pages maintain correct alignment.
- Automatic Page Splitting: Each page is divided exactly in half by its width.
- Rotation Handling: The script respects any rotation metadata, ensuring that the output looks correct.
- Metadata Preservation: Basic document metadata is copied to the new file.
- Customizable Output: You can modify the order or add features like bookmark copying if needed.
-
Reading and Writing PDFs: The script uses
PdfReaderto open the input file andPdfWriterto create the new output file. -
Splitting Pages: The
split_page_into_left_right()function calculates the midpoint of the page and creates two rectangular regions usingRectangleObject. It then crops the original page to create two new pages — one for the left half and one for the right. -
Rotation Support: Pages with 90°, 180°, or 270° rotation are processed appropriately to ensure the left and right halves are extracted visually correctly.
-
Output Creation: The script adds both new pages (left and right) into the output file sequentially and saves it.
To run the script, use the following command in your terminal:
python split_pdf_halves.py input.pdf output.pdfinput.pdf– the path to your source PDF file.output.pdf– the desired name for the new split PDF.
If the input file is missing or the arguments are incorrect, the script will display usage instructions.
Before running, install the required library:
pip install pypdfThis Python script offers a quick, reliable way to split PDF pages into left and right halves. It’s especially useful for digitizing double-page scans or reorganizing content for better readability. With a few small modifications, you can extend it to handle bookmarks, annotations, or even custom split ratios, making it a flexible tool for PDF processing workflows.