[Python-PDF]Split pdf

Amazing lifestyle
3 min readJan 11, 2024

The splitting here is not splitting by page, but splitting a certain page into several pages. For example, each PDF page is divided into two and then reorganized into a new PDF file.

The pdf_2_img function in the program code accepts two parameters: pdfPath is the path of the PDF file to be processed, and imagePath is the storage path of the split image file. This function uses the fitz library to open the PDF file, then iterates through each page and splits each page into two image files, the upper half and the lower half. These image files are saved in the specified directory in JPG format.

The img_2_pdf function accepts a parameter imagePath, which is the storage path of the split image file. This function will traverse the image files in the specified directory, re-merge them into a new PDF file, and save the new PDF file as “new.pdf”.

In the main function, we specify the PDF file path to be processed and the image file storage path. Then, we call the pdf_2_img function to split the PDF file into image files, and then call the img_2_pdf function to merge the split image files back into PDF files.

Overall, this code provides the function of splitting PDF files into image files and remerging them, which can easily handle situations where image processing of PDF files is required.

import fitz
import os…

--

--