In this post we will be creating a Python script that will merge the contents of two PDF files and save it as a new file, to do this we will be using the PDF reader from the PyPDF2 library. The PyPDF2 is a Python library that enables users to perform various operations on PDF documents. It provides a range of functions for reading, editing, and manipulating PDF files, such as merging and splitting PDFs, extracting text and images, and encrypting and decrypting content.
See the sample of Python code below where we utilize the PyPDF2 module to merge our two PDF files, we start by initiating our PDF reader to read from each of the files. We then create a new PDF file and write the merged content to it. In this example the contents of ‘file1.pdf’ will come first within our merged file.
from PyPDF2 import PdfReader, PdfWriter pdf1 = PdfReader("file1.pdf") pdf2 = PdfReader("file2.pdf") with open("merged_file.pdf", "wb") as file: pdf_writer = PdfWriter() for page in pdf1.pages: pdf_writer.add_page(page) for page in pdf2.pages: pdf_writer.add_page(page) pdf_writer.write(file)
Take a look at some of our other content around the Python programming language by clicking here.