Skip to content

Instantly share code, notes, and snippets.

@ericmjl
Created June 5, 2015 16:50
Show Gist options
  • Save ericmjl/19733fc6a14a2be85b4f to your computer and use it in GitHub Desktop.
Save ericmjl/19733fc6a14a2be85b4f 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