-
Notifications
You must be signed in to change notification settings - Fork 0
/
XLSX_JSON_CONVERTER.py
65 lines (51 loc) · 1.99 KB
/
XLSX_JSON_CONVERTER.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
58
59
60
61
62
63
64
#!/usr/bin/env python
# coding: utf-8
from queue import Empty
import openpyxl
import numpy as np
import os
import json
import csv
import pandas as pd
from csv import DictReader
versioning = pd.__version__
working_path = os.getcwd()
csv_folder = os.path.join(working_path,'csv_folder')
excel_folder = os.path.join(working_path,'excel_folder')
output_folder = os.path.join(working_path,'output')
print("Running Version Pandas :%s"%(versioning))
print('Im ready to convert sir')
xlsx_fileList = [f for f in os.listdir(excel_folder) if f.endswith('.xlsx')]
csv_fileList = [f for f in os.listdir(csv_folder) if f.endswith('.csv')]
if xlsx_fileList:
print(f'{xlsx_fileList} ready to be converted')
else:
print('No excel files available')
if csv_fileList:
print(f'{csv_fileList} ready to be converted')
else:
print('No csv files available')
if xlsx_fileList:
for sheet in xlsx_fileList:
fileNameExcel = str(sheet)[:-5]
#define total row to read , if all remove nrows=xxxx,
#dtypes added to avoid missing leading zeros, hence added dtype =str assuming all values a string
df = pd.read_excel(excel_folder+'/'+sheet,nrows=10, dtype=str)
os.chdir(output_folder)
df.to_json("{}.json".format(fileNameExcel), orient='records', indent=2)
jsonlist = [f for f in os.listdir(output_folder) if f.endswith('json')]
print('Conversion to json {} success'.format(sheet))
else:
print('No excel to be converted')
if csv_fileList:
for sheet in csv_fileList:
fileNameExcel = str(sheet)[:-5]
df = pd.read_csv(csv_folder+'/'+sheet,nrows=10, dtype=str)
os.chdir(output_folder)
df.to_json("{}.json".format(fileNameExcel), orient='records', indent=2)
jsonlist = [f for f in os.listdir(output_folder) if f.endswith('json')]
print('Conversion to json {} success'.format(sheet))
else:
print('No csv to be converted')
df3 = pd.DataFrame (jsonlist, columns = ['List of json Available '])
df3