Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize pdf File cutting and merging by ​ PyPDF2 of python

Shulou Source: shulou.com Published: 2022-06-01 18:19:44 09月22日 Update

This article will explain in detail about python PyPDF2 how to achieve pdf file cutting and merging, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

After some time in Baidu, I found that most of them are tailored using Adobe Acrobat software, which is not Pythonic at all, so I found the method of dealing with PDF files with Python, and finally found the PyPDF2 library. This paper will use this library to achieve the segmentation of PDF.

First, you need to install the library through pip:

Pip install PyPDF2

To use the python

# Import read / write pdf module from PyPDF2 import PdfFileReader, PdfFileWriter''' Note: the number of pages starts from 0. Index range () is the left closed and right open interval''def split_pdf (file_name, start_page, end_page). Output_pdf):'': param file_name: pdf file name to be split: param start_page: number of pages at the beginning of the split: param end_page: number of pages at the end of the split: param output_pdf: save the file name after the cut''# read the pdf file to be split input_file = PdfFileReader (open (file_name) 'rb')) # example A PDF file writer output_file = PdfFileWriter () # add split files together for i in range (start_page, end_page): output_file.addPage (input_file.getPage (I)) # Save the split file output with open (output_pdf,' wb') as f: output_file.write (f) def merge_pdf (merge_list Output_pdf): "" merge_list: pdf list to be merged output_pdf: merged pdf name "" # instance A PDF file writer output = PdfFileWriter () for ml in merge_list: pdf_input = PdfFileReader (open (ml) 'rb')) page_count = pdf_input.getNumPages () for i in range (page_count): output.addPage (pdf_input.getPage (I)) output.write (open (output_pdf,' wb')) if _ _ name__ ='_ main__': # split pdf split_pdf ("test.pdf", 0,3, "0-2.pdf") split_pdf ("test.pdf") 7, 12, "7-11.pdf") split_pdf ("test.pdf", 18, 23, "18-22.pdf") split_pdf ("test.pdf", 27, 33, "26-32.pdf") split_pdf ("test.pdf", 40, 44, "40-43.pdf") split_pdf ("test.pdf", 46, 51, "46-50.pdf") split_pdf ("test.pdf", 58, 66) "58-65.pdf") split_pdf ("test.pdf", 77, 84, "77-83.pdf") split_pdf ("test.pdf", 93,97, "93-96.pdf") split_pdf ("test.pdf", 102,106, "102-105.pdf") # merge pdf # merged pdf list pdf_list = ["0-2.pdf", "7-11.pdf" "18-22.pdf", "26-32.pdf", "40-43.pdf", "46-50.pdf", "58-65.pdf", "77-83.pdf", "93-96.pdf", "102-105.pdf"] merge_pdf (pdf_list, "all.pdf")

The following is the sliced file effect, perfect

On "python PyPDF2 how to achieve pdf file cutting and merging" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: File article page instance file name more good practical content effect article method module knowledge index software open area reference processing help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Shulou Technology Xiaomi Shulou Tech Info