From c69cb9f172c5f9e5a0b5183172da52b63a31f299 Mon Sep 17 00:00:00 2001 From: Dominik Rimpf Date: Wed, 30 Oct 2024 08:42:36 +0100 Subject: [PATCH] add interleave script --- interleave.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 interleave.py 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])