-
Notifications
You must be signed in to change notification settings - Fork 1
/
excel2txt.py
46 lines (37 loc) · 1.24 KB
/
excel2txt.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
import argparse
import os
import re
import xlrd
from xlrd.sheet import ctype_text
from os.path import join, dirname, abspath
import re
from os.path import expanduser
import sys
reload(sys)
sys.setdefaultencoding('utf8')
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('input', metavar='Excel File', type=str,
help='the Excel input file')
args = parser.parse_args()
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^\w\s]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
fname = args.input
#fname = join(dirname(dirname(abspath(__file__))), 'amazon_polly', 'textDataset.xlsx')
xl_workbook = xlrd.open_workbook(fname)
xl_sheet = xl_workbook.sheet_by_index(0)
numOfRows = xl_sheet.nrows - 1
home = expanduser("~")
folderPath = home + "/" + "textfaPolly"
if not os.path.exists(folderPath):
os.makedirs(folderPath)
for rows in range(0, numOfRows):
txt = xl_sheet.cell_value(rows, 0)
filename = urlify(txt) + ".txt"
desPath = folderPath + "/" + filename
with open(desPath, 'w+') as my_file:
my_file.write(xl_sheet.cell_value(rows, 0))
exit(folderPath)