Skip to content

Instantly share code, notes, and snippets.

@tuantranf
Forked from ericmjl/merger.py
Created November 29, 2018 07:03
Show Gist options
  • Save tuantranf/80f554a1faba53a9eb0f7bd1f6610428 to your computer and use it in GitHub Desktop.
Save tuantranf/80f554a1faba53a9eb0f7bd1f6610428 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