Merge PDF files using python

  • Post author:
  • Post comments:0 Comments
  • Reading time:18 mins read

Simplify your document management tasks by merging PDF files with the power of Python. In this project, we’ll guide you through the process of creating a Python script to combine multiple PDF documents into one cohesive file.

Whether you’re a professional handling reports, a student organizing study materials, or simply looking to streamline your PDF documents, this step-by-step guide will empower you to merge PDF files effortlessly. Join us on this journey to harness the capabilities of Python for efficient PDF file management and enhance your productivity!


#!/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()
    
Publisher
Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs

Leave a Reply