Merge pdf file using Python Code

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

Streamline your document management with the ability to merge PDF files using Python. In this project, you’ll learn how to create a Python script that combines multiple PDF documents into a single, organized file. Whether you’re a professional working with reports, a student handling study materials, or simply someone looking to manage PDFs efficiently,

This step-by-step guide will empower you to merge PDF files with ease. Join us as we harness the power of Python to simplify your document handling and boost 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