-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathxlstojson.py
43 lines (40 loc) · 1.56 KB
/
xlstojson.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
#!/usr/bin/python3.10
import json, os, sys
from xlsx_reader import get_workbook, get_workbook_data
from xls_reader import get_workbook as xls_get_workbook
from xls_reader import get_workbook_data as xls_get_workbook_data
def get_excel_data_as_json(source_file):
'''Uses either the modern or old reader to get excel data as json'''
pathname = os.path.splitext(source_file)
file_extension = pathname[1]
if file_extension == ".xls":
workbook = xls_get_workbook(source_file)
workbook_data = xls_get_workbook_data(workbook)
else:
workbook = get_workbook(source_file)
workbook_data = get_workbook_data(workbook)
return workbook_data
def main():
''' The CLI / output task. '''
source_file = input("Enter the path to the filename -> ")
if os.path.isfile(source_file):
pathname = os.path.splitext(source_file)
file_name = pathname[0].split('/')[-1]
try:
output_file_name = file_name + '.json'
workbook_data = get_excel_data_as_json(source_file)
with open(output_file_name, 'w+', encoding="utf-8") as output_file:
output_file.write(json.dumps(
workbook_data,
sort_keys=True,
indent=2,
separators=(",", ": ")
))
print (f"{output_file.name} was created")
except Exception as error:
print("some error occured")
print(error)
sys.exit(2)
else:
print ("Sorry, that was not a valid filename")
main()