Python to Merge PDF Files with Full Source Code

A simple python script which when executed merges two pdfs

Prerequisites:

  • PyPDF2==1.26.0
pip install PyPDF2

Run the Script:

It can be executed by running “python merge_pdfs.py”

python merge_pdfs.py
Code language: CSS (css)

Source Code:

merge_pdfs.py

#!/usr/bin/env python from PyPDF2 import PdfFileMerger # By appending in the end def by_appending(): merger = PdfFileMerger() # Either provide file stream f1 = open("samplePdf1.pdf", "rb") merger.append(f1) # Or direct file path merger.append("samplePdf2.pdf") merger.write("mergedPdf.pdf") # By inserting at after an specified page no. def by_inserting(): merger = PdfFileMerger() merger.append("samplePdf1.pdf") merger.merge(0, "samplePdf2.pdf") merger.write("mergedPdf1.pdf") if __name__ == "__main__": by_appending() by_inserting()
Code language: PHP (php)

Leave a Comment