Gavya Mehta
Gavya Mehta

Reputation: 251

Concatenate multiple Pdf using PyPDF2

I am trying to merge multiple pdfs into one. Following is my code:

import PyPDF2
import os
import sys

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")

pdf_path=r'path'
os.chdir(pdf_path)

output_pdf=input("Enter for output pdf: ")

#Get PDF list
pdfmerge=[]
for pdf_file in os.listdir(pdf_path):
    if pdf_file.endswith('.pdf'):
        pdfmerge.append(pdf_file)

pdfWriter = PyPDF2.PdfFileWriter()

#loop through all PDFs
for pdf_file in pdfmerge:
    pdfFileObj=open(pdf_file,'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

for pageNum in range(pdfReader.numPages):
    pageObj=pdfReader.getPage(pageNum)
    pdfWriter.addPage(pageObj)

#Save PDF
pdfOutput=open(output_pdf+'.pdf','wb')

pdfWriter.write(pdfOutput)
pdfOutput.close()

This code is giving the output pdf but only the last pdf in folder is getting concatenated in the output pdf and the rest are not. What do i change in the code so that all the pdfs in the folder get merged and not just the last pdf

Upvotes: 0

Views: 162

Answers (1)

yvesonline
yvesonline

Reputation: 4837

You're missing an indentation when iterating the pages, it should read:

for pdf_file in pdfmerge:
    pdfFileObj=open(pdf_file,'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

    for pageNum in range(pdfReader.numPages):
        pageObj=pdfReader.getPage(pageNum)
        pdfWriter.addPage(pageObj)

Upvotes: 1

Related Questions