Converting Excel File to JSON File using xlrd module

from xlrd import open_workbook
import json
import os

os.chdir("d:/2018/mayank")
book = open_workbook('demo.xlsx')
sheet = book.sheet_by_index(0)

# read header values into the list
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
print ("keys are", keys)

dict_list = []
for row_index in range(1, sheet.nrows):
    d = {keys[col_index]: sheet.cell(row_index, col_index).value
         for col_index in range(sheet.ncols)}
    dict_list.append(d)

j = json.dumps(dict_list)
with open('data.json', 'w') as f:
    f.write(j)


Comments

Popular posts from this blog

Using Oracle with Python

Using time module