Skip to content

Instantly share code, notes, and snippets.

@danyay
Created August 20, 2016 13:07
Show Gist options
  • Save danyay/a0c32548ddd6868e1cb8b1ed62f703f3 to your computer and use it in GitHub Desktop.
Save danyay/a0c32548ddd6868e1cb8b1ed62f703f3 to your computer and use it in GitHub Desktop.
Crop 4x6" shipping labels out of Amazon FBA PDFs
#!/usr/bin/python
# cropamz.py
# by Dan Nunn
# 2016-08-20
from pyPdf import PdfFileWriter, PdfFileReader
import sys
import os.path
# Default the filename to the first argument, otherwise use package.pdf
filename = "package.pdf" if len(sys.argv) < 2 or sys.argv[1] is None else sys.argv[1]
if filename[-4:] != ".pdf":
filename += ".pdf"
# Make sure that the input file exists
if os.path.isfile(filename) is False:
print "'" + filename + "' does not exist\n\n\tUsage: crop.py [filename.pdf]\n" + \
"\tIf filename is not specified, defaults to package.pdf."
exit(0)
input = PdfFileReader(file(filename, "rb"))
output = PdfFileWriter()
numPages = input.getNumPages()
for i in range(numPages):
page = input.getPage(i)
page.cropBox.lowerLeft = (30, 64)
page.cropBox.upperRight = (487, 368)
page.rotateClockwise(90)
output.addPage(page)
outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment