Arrange this pdf pages in this manner 1, 3, 5 7, 9, 11 13, 15, 17 6, 4, 2 12, 10, 8 18, 16, 14 19, 21, 23 25, 27, 29 31, 33, 35 24, 22, 20 30, 28, 26 36, 34, 32 37, 39, 41 43, 45, 47 (blank) 42, 40, 38 48, 46, 44 (blank)

Reading File
Running Command

cd /tmp/workspace && python3 -c " from pypdf import PdfReader reader = PdfReader('/tmp/workspace/attachments/59b1de28-605e-4014-807d-4ab9e2b51dc2/anat hnf 1-merge.pdf') print(f'Total pages: {len(reader.pages)}') "

Running Command

python3 << 'EOF' from pypdf import PdfReader, PdfWriter import os # Source PDF src = '/tmp/workspace/attachments/59b1de28-605e-4014-807d-4ab9e2b51dc2/anat hnf 1-merge.pdf' reader = PdfReader(src) total = len(reader.pages) print(f"Total pages: {total}") # Page order as specified by user (1-indexed) # Row 1: 1, 3, 5, 7, 9, 11, 13, 15, 17 # Row 2: 6, 4, 2, 12, 10, 8, 18, 16, 14 # Row 3: 19, 21, 23, 25, 27, 29, 31, 33, 35 # Row 4: 24, 22, 20, 30, 28, 26, 36, 34, 32 # Row 5: 37, 39, 41, 43, 45, 47, (blank) # Row 6: 42, 40, 38, 48, 46, 44, (blank) page_order = [ # Row 1 1, 3, 5, 7, 9, 11, 13, 15, 17, # Row 2 6, 4, 2, 12, 10, 8, 18, 16, 14, # Row 3 19, 21, 23, 25, 27, 29, 31, 33, 35, # Row 4 24, 22, 20, 30, 28, 26, 36, 34, 32, # Row 5 (blank = None) 37, 39, 41, 43, 45, 47, None, # Row 6 (blank = None) 42, 40, 38, 48, 46, 44, None, ] print(f"Sequence length: {len(page_order)}") print(f"Non-blank pages: {sum(1 for p in page_order if p is not None)}") # Verify all page numbers are within range for p in page_order: if p is not None and (p < 1 or p > total): print(f"ERROR: page {p} out of range!") # Build output PDF writer = PdfWriter() # We need a blank page to insert where None appears # Get dimensions from first page first_page = reader.pages[0] w = float(first_page.mediabox.width) h = float(first_page.mediabox.height) print(f"Page size: {w} x {h} pts") # Create blank page using reportlab from reportlab.pdfgen import canvas from reportlab.lib.units import pt import io # Create blank page PDF in memory blank_buf = io.BytesIO() c = canvas.Canvas(blank_buf, pagesize=(w, h)) c.showPage() c.save() blank_buf.seek(0) blank_reader = PdfReader(blank_buf) blank_page = blank_reader.pages[0] for p in page_order: if p is None: writer.add_page(blank_page) else: writer.add_page(reader.pages[p - 1]) # convert to 0-indexed os.makedirs('/tmp/workspace/output', exist_ok=True) out_path = '/tmp/workspace/output/anat_hnf_reordered.pdf' with open(out_path, 'wb') as f: writer.write(f) print(f"Done! Output: {out_path}") print(f"Output page count: {len(writer.pages)}") EOF

Running Command

python3 << 'EOF' from pypdf import PdfReader, PdfWriter from reportlab.pdfgen import canvas import io, os src = '/tmp/workspace/attachments/59b1de28-605e-4014-807d-4ab9e2b51dc2/anat hnf 1-merge.pdf' reader = PdfReader(src) total = len(reader.pages) first_page = reader.pages[0] w = float(first_page.mediabox.width) h = float(first_page.mediabox.height) # Create blank page in memory blank_buf = io.BytesIO() c = canvas.Canvas(blank_buf, pagesize=(w, h)) c.showPage() c.save() blank_buf.seek(0) blank_reader = PdfReader(blank_buf) blank_page = blank_reader.pages[0] # Page order (1-indexed, None = blank) page_order = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 6, 4, 2, 12, 10, 8, 18, 16, 14, 19, 21, 23, 25, 27, 29, 31, 33, 35, 24, 22, 20, 30, 28, 26, 36, 34, 32, 37, 39, 41, 43, 45, 47, None, 42, 40, 38, 48, 46, 44, None, ] writer = PdfWriter() for p in page_order: if p is None: writer.add_page(blank_page) else: writer.add_page(reader.pages[p - 1]) os.makedirs('/tmp/workspace/output', exist_ok=True) out_path = '/tmp/workspace/output/anat_hnf_reordered.pdf' with open(out_path, 'wb') as f: writer.write(f) print(f"Success! Output: {out_path}") print(f"Total output pages: {len(writer.pages)}") EOF

Done! The PDF has been reordered exactly as you specified.
Page arrangement applied:
RowPages
11, 3, 5, 7, 9, 11, 13, 15, 17
26, 4, 2, 12, 10, 8, 18, 16, 14
319, 21, 23, 25, 27, 29, 31, 33, 35
424, 22, 20, 30, 28, 26, 36, 34, 32
537, 39, 41, 43, 45, 47, (blank)
642, 40, 38, 48, 46, 44, (blank)
  • Source: 48 pages
  • Output: 50 pages (48 original + 2 blank pages at the end of rows 5 and 6)

anat hnf reordered

PDF Document · PDF

This is a shared conversation. Sign in to Orris to start your own chat.