Skip to content Skip to sidebar Skip to footer

List Of Dictionary To Xlwt

I have a list of dictionary and i want to convert it to excel using xlwt. I'm new to xlwt. Can you help me? Im using it as a function to receive list of dict and convert it to exce

Solution 1:

If somebody need version with HEADERS:

import xlwt

w = xlwt.Workbook()
ws = w.add_sheet('sheet1')

columns = list(data[0].keys())

# write headers in row 0for j, col inenumerate(columns):
    ws.write(0, j, col)

# write columns, start from row 1for i, row inenumerate(data, 1):
    for j, col inenumerate(columns):
        ws.write(i, j, row[col])

w.save('data.xls')

Solution 2:

Make a worksheet. Then use Worksheet.write to fill a cell.

data = [
    {'id':u'1','name':u'Jeff'},
    {'id':u'2','name':'Carlo'},
]

import xlwt

w = xlwt.Workbook()
ws = w.add_sheet('sheet1')

columns = list(data[0].keys()) # list() is not need in Python 2.xfor i, row inenumerate(data):
    for j, col inenumerate(columns):
        ws.write(i, j, row[col])

w.save('data.xls')

Post a Comment for "List Of Dictionary To Xlwt"