Search code examples
pythonjsonxlsxxls

convert xls to json in python


I am trying to convert xls to json and but when I am executing the code it's not giving me the data inside xls sheet, it's only giving me the json structure. Below is the code which I am running, I am not able to understand what modification I should further make in this so that I can get a perfect json file.

Please note - input is in the form of binary stream and output is also in the form of a stream and not file.

    #!/usr/bin/python -u
import sys
import xlrd
import simplejson
from collections import OrderedDict

wb = xlrd.open_workbook(file_contents=sys.stdin.read())

for sheet_index in range(wb.nsheets):
#       print sheet_index
        sh =  wb.sheet_by_index(sheet_index)
       # print "Processing sheet no ", sheet_index
        attributes = sh.row_values(0)
        #print attributes
        rows_list = []
        attr_list = []
       # print attr_list[0]

        for rownum in range(1,sh.nrows):
                row_val_list = sh.row_values(rownum)
                row_dict = OrderedDict()
                for index in range(len(attr_list)):
                        row_dict[attr_list[index]] = row_val_list[index]

                #row_dict['ID'] = row_val_list[0]
                #row_dict['Name'] = row_val_list[1]

                #rows_list.append(row_dict)

        #json_data = simplejson.dumps(rows_list)
        #sys.stdout.write(json_data)
                rows_list.append(row_dict)
                json_data = simplejson.dumps(rows_list)
                sys.stdout.write(json_data)

#       json_data = simplejson.dumps(rows_list)

        #sys.stdout.write(json_data)
~

Any help is much appreciated


Solution

  • here is the correct working python code

    #!/usr/bin/python -u
    import sys
    import xlrd
    import simplejson
    from collections import OrderedDict
    
    wb = xlrd.open_workbook(file_contents=sys.stdin.read())
    
    #print "Sheets are .... ", wb.nsheets
    
    for sheet_index in range(wb.nsheets):
            sh =  wb.sheet_by_index(sheet_index)
    
            if sh.nrows == 0:
                    continue
    
            attr_list = sh.row_values(0)
    
            rows_list = []
    
            for rownum in range(1,sh.nrows):
                    row_values = sh.row_values(rownum)
                     row_dict = OrderedDict()
    
                    for index in range(len(attr_list)):
                            row_dict[attr_list[index]] = row_values[index]
    
    
    
                    rows_list.append(row_dict)
    
            json_data = simplejson.dumps(rows_list)
            sys.stdout.write(json_data)