forked from yuedanlabs/pdf-crop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (43 loc) · 1.39 KB
/
main.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
"""
thanks to https://github.com/py-pdf/PyPDF2
refer to https://pypdf2.readthedocs.io/en/latest/user/cropping-and-transforming.html
refer:
page.mediabox RectangleObject [left, bottom, right, top]
"""
from copy import deepcopy
from itertools import product
from PyPDF2 import PdfWriter, PdfReader
reader = PdfReader("input.pdf")
writer = PdfWriter()
page0 = reader.pages[0]
# split horizontally into 3 equal parts
# divide vertically into 4 equal parts
x, y = (3, 4)
# for j in range(y, 0, -1):
# for i in range(1, x+1):
# pagex = deepcopy(reader.pages[0])
# pagex.mediabox.lower_left = (
# (page0.mediabox.right / x) * (i - 1),
# (page0.mediabox.top / y) * (j - 1),
# )
# pagex.mediabox.upper_right = (
# (page0.mediabox.right / x) * i,
# (page0.mediabox.top / y) * j,
# )
# writer.add_page(pagex)
for j, i in product(range(y, 0, -1), range(1, x + 1)):
# print(i, j)
pagex = deepcopy(reader.pages[0])
# lower_left = (left, bottom)
pagex.mediabox.lower_left = (
(page0.mediabox.right / x) * (i - 1),
(page0.mediabox.top / y) * (j - 1),
)
# upper_right = (right, top)
pagex.mediabox.upper_right = (
(page0.mediabox.right / x) * i,
(page0.mediabox.top / y) * j,
)
writer.add_page(pagex)
with open("output.pdf", "wb") as fp:
writer.write(fp)