Merge individual pages of PDF file into one page with python

Sifat
2 min readJun 22, 2023

--

Simplifying PDF Organization: Merging Multiple Pages into a Single Page using Python

In this tutorial, I will demonstrate how to merge or group multiple PDF pages into a single consolidated page using Python.

PyPDFMerge

PyPDFMerge provides a convenient method for merging individual pages of a PDF document into one cohesive page.

Demo

Merging 2 pages into 1

Installation

To begin using PyPDFMerge, you must first install it on your system. Open a terminal or command prompt and enter the following command:

pip install PyPDFMerge

For detailed installation instructions, refer to the following GitHub repository:

Usage

Assuming you have a PDF named today.pdfyou can utilize the command-line interface to merge its pages.

pdfmerger today.pdf

This command will merge two pages into one and save the result as output.pdf

If you wish to customize the group size or specify an output file, you can use the following command:

pdfmerge today.pdf -o output.pdf -g 4

Python

Alternatively, you can incorporate the PyPDFMerge package into your Python code. It offers the following options:

from pdfmerger import PDFMerge

pdf = PDFMerge(pdf_file=<path>, output_file=<output>, group_size=<group_size>, quality=<quality>, page_number=<page_number>)
pdf.run()

Here’s an example usage:

from pdfmerger import PDFMerge

pdf = PDFMerge(pdf_file="./test.pdf", output_file="./output.pdf", group_size=2, quality=1.5)
pdf.run()

In this example, a PDFMerge object is created with the specified parameters. The pdf_file parameter represents the path to the input PDF file, the output_file parameter specifies the desired output file, the group_size parameter determines the number of pages to merge together, and the quality parameter adjusts the output quality. Once the object is set up, the run() method is called to initiate the merging process.

--

--

Sifat

"I'm Sifat, a computer engineering student who loves bringing ideas to life through coding. You'll find me crafting open-source projects in my free time."