Skip to content

Instantly share code, notes, and snippets.

@MicahElliott
Created November 29, 2010 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MicahElliott/719699 to your computer and use it in GitHub Desktop.
Save MicahElliott/719699 to your computer and use it in GitHub Desktop.
Lay out a linearly paged book for bound printing.
#! /usr/bin/env python
""" Lay out a linearly paged book for bound printing.
Described in this post:
http://micahelliott.com/2008/12/from-e-book-to-real-book/
Additions to post:
* Firefox does not support the layout, so need to generate PDF.
* Dad laughs at my process.
* Mac allows flipping/rotating, would be nice for folding in linux.
* Add some terminology.
* Describe gray-scale, low quality are sufficient and fast.
* Discuss printer ink market scam.
Todo features:
* Start on any given page. Eg, for print a book in two halves.
* Or simplest to just allow a range; eg, 119:220
* Redo algorithm for print full one-sided stack, then flipping to do
other half.
* Specify which page is blank for repeating at end.
* Warnings about pages not divisible by 8.
* Use optparse for start_page, end_page, half_stack, warning.
* Specify "skip" pages for pages/sections to be omitted.
* Specify cover page.
"""
__author__ = 'Micah Elliott http://MicahElliott.com'
__version__ = '0.1'
__copyright__ = 'WTFPL http://sam.zoy.org/wtfpl/'
import sys
USAGE = 'usage: booklayout npages'
try:
orig_nminis = int(sys.argv[1])
except:
print USAGE
raise SystemExit
### Set shrinkage ratio for printing.
mpb = 4 # minis-per-big ratio
mpp = mpb * 2 # minis per (2-sided) page
### Pad blank pages with last page, hoping it's a blank.
#blank_pad = 'BLANK'
blank_pad = orig_nminis
if orig_nminis % mpp == 0:
nminis = orig_nminis
else:
# Division is really a floor.
##23 / 4 * 4 + 4
nminis = orig_nminis / mpp * mpp + mpp
###print nminis
def blank(page):
if page > orig_nminis: return blank_pad
else: return page
pages = []
i, j = 1, nminis
while i < nminis/2:
##16,1 14,3
##2,15 4,13
front = [j, i, j-2, i+2]
back = [i+1, j-1, i+3, j-3]
front_back = [blank(page) for page in front+back]
pages.append( ','.join(map(str, front_back)) )
i += mpb
j -= mpb
### Generate result to send to printer.
# Assuming printer can pad with 'b's.
pages_csv = ','.join(pages)
print pages_csv
### For starting at offset.
# Incomplete, hard-coded.
start_page = 119
pages_lst = pages_csv.split(',')
offset_pages = [str( int(pg) + (start_page-1) ) for pg in pages_lst]
print '\nOffset of', start_page
print ','.join(offset_pages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment