How to merge many pdf at once with python

With this code you can merge all the file in the current directory.

from glob import glob
from PyPDF2 import PdfFileMerger
import os


def pdf_merge(name):
    ''' Merges all the pdf files in current directory '''
    filename = f"{name}.pdf" # add extension to name
    merger = PdfFileMerger()
    allpdfs = [a for a in glob("*.pdf")]
    [merger.append(pdf) for pdf in allpdfs]
    with open(filename, "wb") as new_file:
        merger.write(new_file)
    os.system(f"start {filename}") # opens the file


if __name__ == "__main__":
    pdf_merge(input("Output file name: "))

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.