Python 3 Script to Merge or Combine Multiple PDF Files Using PyPDF2 Library Full Tutorial For Beginners

Python 3 Script to Merge or Combine Multiple PDF Files Using PyPDF2 Library Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be merging or combining multiple pdf files in python 3 using pypdf2 library. All the source code of the project will be given below.

 

 

 

Requirements

 

 

python 3 should be installed on your system

 

pypdf2 library should be installed on your system

 

 

Installation

 

 

In order to install pypdf2 library we can use the pip command to install

 

pip install pypdf2

 

After installing it you can make a app.py file inside the root directory and copy paste the following code

 

 

app.py

 

 

 

# importing required modules 
import PyPDF2 

def PDFmerge(pdfs, output): 
    # creating pdf file merger object 
    pdfMerger = PyPDF2.PdfFileMerger() 
    
    # appending pdfs one by one 
    for pdf in pdfs: 
        with open(pdf, 'rb') as f: 
            pdfMerger.append(f) 
        
    # writing combined pdf to output pdf file 
    with open(output, 'wb') as f: 
        pdfMerger.write(f) 

def main(): 
    # pdf files to merge 
    pdfs = ['create.pdf', 'create2.pdf'] 
    
    # output pdf file name 
    output = 'combined_example.pdf'
    
    # calling pdf merge function 
    PDFmerge(pdfs = pdfs, output = output) 

if __name__ == "__main__": 
    # calling the main function 
    main()

 

READ  Python 3 Script to Convert Celsius to Fahrenheit Temperature Full Project For Beginners

 

 

Leave a Reply