add interleave script

This commit is contained in:
Dominik Rimpf 2024-10-30 08:42:36 +01:00
parent fd1551202c
commit c69cb9f172

38
interleave.py Normal file
View file

@ -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])