title: Merge 2 PDF's in 16 page segments
body: <p>I have 2 PDF's resultant from splitting a 2-up document composed by 32 pages signatures. Meaning one PDF has pages 1-16, 33-48, 65-80.... and the other has pages 17-32, 49-64, 81-96....</p>
  <p>How can I merge both, iterating through 16-page segments of each, using Python? To get a final composed PDF with 1-16, 17-32, 33-48, 49-64.....</p>
  <p>I can iterate them page by page and I can combine one full PDF after the other, etc. But can't seem to get the correct way merging by segments.</p>
  <p>The first operations are done with external software (Xerox Freeflow Core) and I get to a point where I have 4 files with the 16-page sequences divided in even/odd pages, I join them iterating with:</p>
  <pre><code>import itertools as itt
  import sys
  import PyPDF2 as PDF
  def main():
  fbase = sys.argv[1]
  
  pdf_out = PDF.PdfFileWriter()
  
  with open(fbase + &quot;_odd.pdf&quot;, 'rb') as f_odd:
      with open(fbase + &quot;_even.pdf&quot;, 'rb')  as f_even:
          pdf_odd = PDF.PdfFileReader(f_odd)
          pdf_even = PDF.PdfFileReader(f_even)
  
          for p in itt.chain.from_iterable(
              itt.zip_longest(
                  pdf_odd.pages,
                  (pdf_even.pages),
              )
          ):
              if p:
                  pdf_out.addPage(p)
  
          with open(fbase + &quot;.pdf&quot;, 'wb') as f_out:
              pdf_out.write(f_out)
  
  return 0
  if __name__ == &quot;__main__&quot;:
  
  if len(sys.argv) != 2:
      print(&quot;Wrong number of arguments!&quot;)
      sys.exit(1)
  
  sys.exit(main())
  </code></pre>
  <p>Afterwards I get the 2 files mentioned above. The code above would work for me if I could iterate through 16p segments instead of page by page.</p>
  <p>Any clues, please?</p>
  <p>Thanks</p>