Skip to content

Instantly share code, notes, and snippets.

@citrus-lemon
Created June 12, 2016 16:04
Show Gist options
  • Save citrus-lemon/593ec427338c2569d169996e568d1757 to your computer and use it in GitHub Desktop.
Save citrus-lemon/593ec427338c2569d169996e568d1757 to your computer and use it in GitHub Desktop.
from __future__ import division
import copy
import math
import pyPdf
import sys,os
# import pyPdf.generic.RectangleObject as rr
def split_pages(src, dst):
src_f = file(src, 'r+b')
dst_f = file(dst, 'w+b')
input = pyPdf.PdfFileReader(src_f)
output = pyPdf.PdfFileWriter()
# print input.getNumPages()
# print input.getPage(0).mediaBox.upperRight
if input.isEncrypted:
print input.decrypt("sem")
x3, x4 = input.getPage(0).mediaBox.upperRight
x3 = float(x3)
x4 = float(x4)
print x3, x4
list = [pyPdf.generic.RectangleObject([90/805.9*x3,299/559*x4,352/805.9*x3,500/559*x4])
,pyPdf.generic.RectangleObject([452/805.9*x3,299/559*x4,714/805.9*x3,500/559*x4])
,pyPdf.generic.RectangleObject([90/805.9*x3,66/559*x4,352/805.9*x3,267/559*x4])
,pyPdf.generic.RectangleObject([452/805.9*x3,66/559*x4,714/805.9*x3,267/559*x4])]
# print list
for i in range(input.getNumPages()):
p = input.getPage(i)
x5, x6 = math.floor(x3/2), math.floor(x4/2)
for j in range(0,4):
q = copy.copy(p)
q.mediaBox = list[j]
# print q.mediaBox
if (j in range(3,4)) and (i == input.getNumPages()-1):
pass
else:
output.addPage(q)
# q = copy.copy(p)
# q.mediaBox = copy.copy(p.mediaBox)
#
# x1, x2 = p.mediaBox.lowerLeft
# x3, x4 = p.mediaBox.upperRight
#
# x1, x2 = math.floor(x1), math.floor(x2)
# x3, x4 = math.floor(x3), math.floor(x4)
# x5, x6 = math.floor(x3/2), math.floor(x4/2)
#
# if x3 > x4:
# # horizontal
# p.mediaBox.upperRight = (x5, x4)
# p.mediaBox.lowerLeft = (x1, x2)
#
# q.mediaBox.upperRight = (x3, x4)
# q.mediaBox.lowerLeft = (x5, x2)
# else:
# # vertical
# p.mediaBox.upperRight = (x3, x4)
# p.mediaBox.lowerLeft = (x1, x6)
#
# q.mediaBox.upperRight = (x3, x6)
# q.mediaBox.lowerLeft = (x1, x2)
# output.addPage(p)
# output.addPage(q)
output.write(dst_f)
src_f.close()
dst_f.close()
split_pages(sys.argv[1],sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment