【问题标题】:using xlwt to filling in a cell with color and a cell without color python使用 xlwt 填充一个有颜色的单元格和一个没有颜色的单元格 python
【发布时间】: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


【解决方案1】:

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

if values=='w':

你会的

if data=='w':

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多