-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit_pdf_functionality.py
More file actions
58 lines (41 loc) · 1.8 KB
/
split_pdf_functionality.py
File metadata and controls
58 lines (41 loc) · 1.8 KB
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
58
# importing the required modules
import PyPDF2
def split_pdf(file_path,save_path,save_name):
def PDFsplit(pdf, splits, pdfFileObj, pdfReader):
# starting index of first slice
start = 0
# starting index of last slice
end = splits[0]
for i in range(len(splits)):
# creating pdf writer object for (i+1)th split
pdfWriter = PyPDF2.PdfFileWriter()
# output pdf file name
outputpdf = save_path+"/"+save_name +"("+ str(i) +")"+ '.pdf'
#print(outputpdf)
# adding pages to pdf writer object
for page in range(start,end):
pdfWriter.addPage(pdfReader.getPage(page))
# writing split pdf pages to pdf file
with open(outputpdf, "wb") as f:
pdfWriter.write(f)
# interchanging page split start position for next split
start = end
try:
# setting split end position for next split
end = splits[i+1]
except IndexError:
# setting split end position for last split
end = pdfReader.numPages
# closing the input pdf file object
pdfFileObj.close()
#print("splited.")
# pdf file to split
pdf = file_path
# creating input pdf file object
pdfFileObj = open(pdf, 'rb')
# creating pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# split page positions///[split from # page,Total no. of pages]
splits = [1,pdfReader.numPages]
# calling PDFsplit function to split pdf
PDFsplit(pdf, splits, pdfFileObj, pdfReader)