- Merge 0001.pdf and 0002.pdf into one.
- Make a list of indexes. Interleave two indexes. First one is 0 to 19. Second one is reversed as 39 to 20.
- Rearrange the merged.pdf as the ordered_indexes list.
I scanned both-sided papers into two pdf files, front pages and reversed back pages. Merge and rearrange pages from two pdf files into one pdf file.
script methods
tools
RearrangePdf-PdfFilePath_Output-PdfFilePath_OrdersList_Input.py
In [4]: from PyPDF2 import PdfFileMerger
In [6]: merger = PdfFileMerger()
In [11]: pdfs = ['/Users/.../Downloads/0001.pdf', '/Users/.../Downloads/0002.pdf']
In [12]: for f in pdfs: merger.append(f)
In [13]: merger.write("a.pdf")
In [14]: merger.close()
In [15]: from PyPDF2 import PdfFileReader, PdfFileWriter
In [16]: reader = PdfFileReader('a.pdf')
In [35]: f_page_idxes = range(20) ; b_page_idxes = reversed(range(20, 40)) ; fb_page_idxes = zip(f_page_idxes, b_page_idxes)
In [41]: ordered_idxes = list(itertools.chain(*fb_page_idxes))
In [42]: writer = PdfFileWriter()
In [43]: for p in ordered_idxes: writer.addPage(reader.getPage(p))
In [45]: with open("arranged_pdf.pdf", "wb") as f: writer.write(f)