【发布时间】:2019-07-26 11:10:59
【问题描述】:
我有我的清单
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 中每个元素的迭代
【问题讨论】:
-
我忘记了部分代码 # Workbook is created wb = Workbook() # add_sheet 用于创建工作表。 sheet1 = wb.add_sheet('表 1')
标签: python-3.x loops for-loop iteration xlwt