-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfSplit.py
53 lines (47 loc) · 1.3 KB
/
pdfSplit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import copy
import math
import pyPdf
import os
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()
for i in range(input.getNumPages()):
page1 = input.getPage(i)
page2 = copy.copy(page1)
page2.mediaBox = copy.copy(page1.mediaBox)
#x1,y2--x2,y2
#| |
#| |
#x1,y3--x2,y3
#| |
#| |
#x1,y1--x2,y1
#x1,y2---x3,y2---x2,y2
#| | |
#| | |
#| | |
#x1,y1---x3,y1---x2,y1
x1, y1 = page1.mediaBox.lowerLeft
x2, y2 = page1.mediaBox.upperRight
x1, y1 = math.floor(x1), math.floor(y1)
x2, y2 = math.floor(x2), math.floor(y2)
x3, y3 = math.floor(x2/2), math.floor(y2/2)
if x2 < y2:
# vertical
page1.mediaBox.upperRight = (x2, y2)
page1.mediaBox.lowerLeft = (x1, y3)
page2.mediaBox.upperRight = (x2, y3)
page2.mediaBox.lowerLeft = (x1, y1)
else:
# horizontal
page1.mediaBox.upperRight = (x3, y2)
page1.mediaBox.lowerLeft = (x1, y1)
page2.mediaBox.upperRight = (x2, y2)
page2.mediaBox.lowerLeft = (x3, y1)
output.addPage(page1)
output.addPage(page2)
output.write(dst_f)
src_f.close()
dst_f.close()