Skip to content

Instantly share code, notes, and snippets.

@pschwede
Created February 11, 2011 21:11
Show Gist options
  • Save pschwede/823036 to your computer and use it in GitHub Desktop.
Save pschwede/823036 to your computer and use it in GitHub Desktop.
Rearranges pages for book-like fold-able printouts.
#!/usr/bin/env python
"""
This script calculates and returns a string of page numbers for
printing book-like fold-able printouts.
Use it like:
python pages.py 42 > pages.file
pdftk input.pdf cat `cat pages.file` output output.pdf
NOTE: Only first parameter is recognized when executed.
Thanks to http://ubuntuforums.org/showthread.php?t=868950 for the
inspiration.
"""
from sys import argv
def pages(num, topfirst=True, seperator=" "):
"""
num = number of pages
topfirst = whether you like to print with or without turning the sheet
on each printed front (Default: True)
seperator = string between the page numbers (Default: ' ')
It basically continues (4,1,2,3,...) which are the four sides of one
printout and adds remaining pages.
"""
order = (4, 1, 2, 3,)
a = ""
b = ""
c = ""
for i in range(num/4):
a += str(order[0]+4*i)+seperator
a += str(order[1]+4*i)+seperator
b += str(order[2]+4*i)+seperator
b += str(order[3]+4*i)+seperator
for o in order:
c += str(o+4*i)+seperator
for i in range(num%4):
b += str(num-num%4+i)+seperator
c += str(num-num%4+i)+seperator
if topfirst:
return a+b
else:
return c
if __name__ == "__main__":
if len(argv):
print pages(int(argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment