Skip to content

Instantly share code, notes, and snippets.

@akandratovich
Created September 23, 2011 12:43
Show Gist options
  • Save akandratovich/1237245 to your computer and use it in GitHub Desktop.
Save akandratovich/1237245 to your computer and use it in GitHub Desktop.
Script for cropping pdf books
from pyPdf import PdfFileWriter as pw, PdfFileReader as pr
# crop in_file
in_file = 'file.pdf'
out_file = 'file.crop.pdf'
out_pdf = pw()
in_pdf = pr(file(in_file, 'rb'))
for p in range(in_pdf.getNumPages()):
page = in_pdf.getPage(p)
# [0, 0, 612, 792]
mb = page.mediaBox
mb.setUpperRight((420, 540))
mb.setLowerLeft((80, 20))
out_pdf.addPage(page)
out_pdf.write(file(out_file, 'wb'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment