-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_pdfXSplitter.py
42 lines (33 loc) · 1.44 KB
/
Test_pdfXSplitter.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
from pyPdf import PdfFileWriter, PdfFileReader
import os
from glob import glob
def get_file():
lstFile = glob('resource\*.pdf')
return lstFile
def splitXPDF(pdfFileName):
try:
inputpdf = PdfFileReader(open(pdfFileName , "rb"))
print '[+] Total Page : ' + str(inputpdf.getNumPages())
setpath = pdfFileName[pdfFileName.find('\\')+1:pdfFileName.find('.')]
lstName = []
with open("nameFile.base", "r") as nameFile:
lstName = nameFile.read().split('\n')
if(inputpdf.getNumPages() == len(lstName)):
for i in xrange(inputpdf.numPages):
output = PdfFileWriter()
output.addPage(inputpdf.getPage(i))
if(os.path.isdir('resault') != True):
os.mkdir('resault')
if(os.path.isdir('resault\\'+setpath) != True):
os.mkdir('resault\\'+setpath)
with open('resault\\' + setpath + '\\' + lstName[i] + '.pdf' , 'wb') as outputStream:
output.write(outputStream)
print '[+] Generate Page '+ str(i+1) + ' with File : ' + lstName[i] + '.pdf'
else:
print '[-] Number of Name in \'nameFile.base\' is not match with Number Page in PDF.'
except IOError:
print '[-] Cannot Openfile.'
if __name__ == '__main__':
AllFile = get_file()
for fly in AllFile:
splitXPDF(fly)