Skip to content

Instantly share code, notes, and snippets.

@matthewhochler
Forked from ericmjl/merger.py
Created July 24, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhochler/748b99e10e5038badbc438d7df478ce6 to your computer and use it in GitHub Desktop.
Save matthewhochler/748b99e10e5038badbc438d7df478ce6 to your computer and use it in GitHub Desktop.
A Python script for merging PDF files together.
"""
Author: Eric J. Ma
Purpose: To merge PDFs together in an automated fashion.
"""
import os
from PyPDF2 import PdfFileReader, PdfFileMerger
files_dir = os.getcwd()
all_files = list()
# Add in main text file.
main_text = [f for f in os.listdir(files_dir) if 'Draft Text' in f and 'pdf' in f]
all_files.extend(main_text)
# Add in Figures.
figures = sorted([f for f in os.listdir(files_dir) if 'Figure' in f and 'pdf' in f])
all_files.extend(figures)
# Add in Extended Data
ext_data = sorted([f for f in os.listdir(files_dir) if 'Extended Data' in f and 'pdf' in f])
all_files.extend(ext_data)
# Add in Supplementary Data
supp_data = sorted([f for f in os.listdir(files_dir) if 'Supplementary Data' in f and 'pdf' in f])
all_files.extend(supp_data)
# Merge the files
merger = PdfFileMerger()
for f in all_files:
merger.append(PdfFileReader(f), 'rb')
merger.write('EJM_Reassortment.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment