使用 xlwt 填充有颜色的单元格和没有颜色的单元格 python

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

我有我的清单

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', '', '', '', '', '']]

我只想为列表中的元素着色 'w' 我有一个代码,但它为所有列着色,而不仅仅是为 ''

的列着色
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') 

不确定我是否错过了值中每个元素的迭代

您正在使用 values 并且应该使用 data 来引用内部列表中的单个元素,因此不要使用:

if values=='w':

你会

if data=='w':