-
Notifications
You must be signed in to change notification settings - Fork 1
/
PDF_Split.py
58 lines (38 loc) · 1.41 KB
/
PDF_Split.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
54
55
56
57
from PyPDF2 import PdfFileWriter, PdfFileReader
import os
def split(pdf, page_num):
pdf_lis = pdf.split("/")
pdf_lis1 = pdf.split("/")
pdf_lis.remove(pdf_lis[-1])
print(pdf_lis)
new_path = "/".join(pdf_lis) + "/split/"
filename = pdf_lis1[-1]
print (filename)
try:
os.mkdir(new_path)
except Exception:
pass
pdf_reader = PdfFileReader(open(pdf, "rb"))
mid = round(page_num/3)
pdf_writer = PdfFileWriter()
for i in range(0,mid):
pdf_writer.addPage(pdf_reader.getPage(i))
# output.addPage(pdf)
with open(new_path + str(filename).lower().replace(".pdf", "_1.pdf"), "wb") as outputStream:
pdf_writer.write(outputStream)
outputStream.close()
pdf_writer = PdfFileWriter()
for i in range(mid, mid+mid):
pdf_writer.addPage(pdf_reader.getPage(i))
# output.addPage(pdf)
with open(new_path + str(filename).lower().replace(".pdf", "_2.pdf"), "wb") as outputStream1:
pdf_writer.write(outputStream1)
outputStream1.close()
pdf_writer = PdfFileWriter()
for i in range(mid+mid, page_num):
pdf_writer.addPage(pdf_reader.getPage(i))
# output.addPage(pdf)
with open(new_path + str(filename).lower().replace(".pdf", "_3.pdf"), "wb") as outputStream1:
pdf_writer.write(outputStream1)
outputStream1.close()
def merge(pdf): pass