Search code examples
python-3.xloopsfor-loopiterationxlwt

using xlwt to filling in a cell with color and a cell without color python


I have my list

mylist=[['w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', '', '', '', '', ''],['', '', '', '', '', '', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', '', '', '', '', '']]

I want to just color the elem in the list that are 'w' I have a code but its coloring all of the columns instead of just the ones that are ''

import xlwt 
from xlwt import Workbook 

row = 1
for values in my_list:
    for col, data in enumerate(values):
        if values=='w':
            style=xlwt.easyxf('pattern: pattern solid, fore_colour blue;' 'font: colour black, bold True, name Calibri, height 180; align: vert centre, horiz centre;border: left thin,right thin,top thin,bottom thin') 
            sheet1.write(6+row, 1+col, data, style=style) 
        else:
            style=xlwt.easyxf('pattern: pattern solid, fore_colour white;' 'font: colour black, bold True, name Calibri, height 180; align: vert centre, horiz centre;border: left thin,right thin,top thin,bottom thin') 
            sheet1.write(6+row, 1+col, data, style=style) 

    row = row + 1 

wb.save('example.xls') 

Not sure if I missed a iteration of the each element in values


Solution

  • You are using values and should be using data to refer to a single element in your inner list, so instead of:

    if values=='w':
    

    you would have

    if data=='w':