Skip to content

Instantly share code, notes, and snippets.

@guori12321
Last active August 29, 2015 14:05
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 guori12321/9ad80a642ab0728c5e4e to your computer and use it in GitHub Desktop.
Save guori12321/9ad80a642ab0728c5e4e to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from pyPdf import PdfFileWriter, PdfFileReader
import glob
for filename in glob.glob(r'./in/*'):
print filename
input1 = PdfFileReader(file(filename, "rb"))
input2 = PdfFileReader(file(filename, "rb"))
input3 = PdfFileReader(file(filename, "rb"))
input4 = PdfFileReader(file(filename, "rb"))
output = PdfFileWriter()
numPages = input1.getNumPages()
print "document has %s pages." % numPages
for i in range(numPages):
page = input1.getPage(i)
print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
#0
page.trimBox.lowerLeft = (0, 0)
page.trimBox.upperRight = (612, 792)
#Left, Down
page.cropBox.lowerLeft = (100, 327)
#Right, Up
page.cropBox.upperRight = (360, 525)
output.addPage(page)
page1 = input2.getPage(i)
#1
page1.trimBox.lowerLeft = (0, 0)
page1.trimBox.upperRight = (612, 792)
#Left, Down
page1.cropBox.lowerLeft = (690, 327)
#Right, Up
page1.cropBox.upperRight = (430, 527)
output.addPage(page1)
page1 = input3.getPage(i)
#3
page1.trimBox.lowerLeft = (0, 0)
page1.trimBox.upperRight = (612, 792)
#Left, Down
page1.cropBox.lowerLeft = (100, 88)
#Right, Up
page1.cropBox.upperRight = (360, 288)
output.addPage(page1)
page1 = input4.getPage(i)
#3
page1.trimBox.lowerLeft = (0, 0)
page1.trimBox.upperRight = (612, 792)
#Left, Down
page1.cropBox.lowerLeft = (690, 88)
#Right, Up
page1.cropBox.upperRight = (430, 288)
output.addPage(page1)
outputStream = file("./out/Cutted_%s.pdf" % filename[5:], "wb")
output.write(outputStream)
outputStream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment