Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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