Merge All PDFs In A Directory Into One PDF File With Python

Do you need to merge all the PDFs inside a directory into a single PDF file? Then I've got a simple python script for you.

Requirements

  • General Knowledge of Python
  • Python 3.6 or higher installed
  • PyPDF2 Library Installed
  • A directory with multiple PDF files you need merged

The Script

Save the script in the folder/directory that contains the multiple PDF files you want to merge and run the script from that folder.

#!/usr/bin/env python3

# Merge PDFs - Take all PDFs in a directory and merge them into one PDF.
# USAGE: Place the script in a directory with PDFs to combine and run.

import PyPDF2
import os

pdfFiles = []
for filename in os.listdir('.'):
    if filename.endswith('.pdf'):
        pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)

pdfWriter = PyPDF2.PdfFileWriter()

for filename in pdfFiles:
    pdfFileObj = open(filename, 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    for pageNum in range(0, pdfReader.numPages):
        pageObj = pdfReader.getPage(pageNum)
        pdfWriter.addPage(pageObj)

pdfOutput = open('combinedPDF_rename.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()


You'll only receive email when Sojourner publishes a new post

More fromĀ Sojourner