-
Notifications
You must be signed in to change notification settings - Fork 1
/
hotdogbun.py
48 lines (37 loc) · 1.45 KB
/
hotdogbun.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
import sys
from PyPDF2 import PdfFileWriter, PdfFileReader
def split(paperpdf, splitpdf):
output = PdfFileWriter()
with open(paperpdf, "rb") as l:
with open(paperpdf, "rb") as r:
# I know... I know.
# We have to do this because PyPDF2 kind of sucks.
left = PdfFileReader(l)
right = PdfFileReader(r)
pagecount = left.getNumPages()
print("%s has %s pages to split." % (paperpdf,pagecount))
for num in range(0, pagecount):
left_page = left.getPage(num)
right_page = right.getPage(num)
midpoint = (
left_page.mediaBox.getUpperRight_x() / 2,
left_page.mediaBox.getUpperRight_y()
)
left_page.mediaBox.upperRight = midpoint
output.addPage(left_page)
right_page.mediaBox.upperLeft = midpoint
output.addPage(right_page)
print("Writing %s pages to %s" % (output.getNumPages(), splitpdf))
with open(splitpdf, "wb") as s:
output.write(s)
def main(argv):
# options are for suckers
pdfs = argv[1:]
for original in pdfs:
if original.endswith('.pdf'):
splitpdf = original[:-4] + '-split.pdf'
else:
splitpdf = original + '-split.pdf'
split(original, splitpdf)
if __name__ == '__main__':
main(sys.argv)