diff --git a/interleave.py b/interleave.py new file mode 100644 index 0000000..a80cb75 --- /dev/null +++ b/interleave.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +import os +import sys + +from pdfrw import PdfReader, PdfWriter + + +def main(input_files, output_file): + cwd = os.getcwd() + + all_pdf_pages = {} + lengths = [] + print(f'loading input files: {input_files}') + for pdf_filename in input_files: + reader = PdfReader(os.path.join(cwd, pdf_filename)) + all_pdf_pages[pdf_filename] = reader.pages + lengths.append(len(reader.pages)) + + assert all([x == lengths[0] for x in lengths]) + + interleaved_pdf = PdfWriter() + for i in range(lengths[0]): + for pdf in all_pdf_pages.values(): + interleaved_pdf.addpage(pdf[i]) + + interleaved_pdf.write(os.path.join(cwd, output_file)) + + +def usage(): + print('Usage: interleave.py output.pdf input1.pdf input2.pdf [input3.pdf ...]') + + +if __name__ == '__main__': + if len(sys.argv) < 4: + usage() + sys.exit(1) + + main(sys.argv[2:], sys.argv[1])