Skip to content

Instantly share code, notes, and snippets.

@danperrout
Created July 15, 2020 13:33
Show Gist options
  • Save danperrout/1fa3a714541bd5b96a876a09556448c6 to your computer and use it in GitHub Desktop.
Save danperrout/1fa3a714541bd5b96a876a09556448c6 to your computer and use it in GitHub Desktop.
Merge two or more PDF with Python - Easy and simple
# Merging two or more PDF files
# Install PyPDF2 with pip
# ´pip install PyPDF2´
# Save this file on the folder and run it on the terminal with ´python merge.py´
import glob
from PyPDF2 import PdfFileMerger
def merger(output_path, input_paths):
pdf_merger = PdfFileMerger()
file_handles = []
for path in input_paths:
pdf_merger.append(path)
with open(output_path, 'wb') as fileobj:
pdf_merger.write(fileobj)
if __name__ == '__main__':
paths = glob.glob('*.pdf')
paths.sort()
merger('output.pdf', paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment