求大神指点,python脚本文件,设置EXCEL单元格背景颜色,并没有报错,但是为什么不变色

#coding :utf-8
import time
import xlsxwriter
import xlrd

class Loginfo(object):
def init(self,path='',mode = 'w'):
fname = path + time.strftime('%Y-%m-%d', time.gmtime())
self.log = open(path + fname + '.txt', mode)
def log_write(self,msg):
self.log.write(msg)
def log_close(self):
self.log.close()

def log_init(self, sheetname, *title):
    pass

class xlloginfo(object):
def init(self, path=''):
fname = path + time.strftime('%Y-%m-%d', time.gmtime())
self.row = 0
self.xl = xlsxwriter.Workbook(path+fname+'.xls')
self.style = self.xl.add_format({'bg_color':'red'})
def xl_write(self, *args):
col = 0
style = ''
if 'Error' in args:
style = self.style
for val in args:
self.sheet.write_string(self.row, col, val)
col += 1
self.row+=1
def log_init(self, sheetname, *title):
self.sheet = self.xl.add_worksheet(sheetname)
self.sheet.set_column('A:E', 30)
self.xl_write(*title)

def log_write(self, *args):
    self.xl_write(*args)
def log_close(self):
    self.xl.close()

if name == '__main__':
'''
log = Loginfo()
log.log_write('test Loginfo 测试')
log.log_close()
'''

xinfo = xlloginfo()
xinfo.log_init('test', 'uname', 'pwd', 'result', 'info')
xinfo.log_write('123', '123', 'Error', 'error')
xinfo.xl.close()

1个回答

style只定义没使用当然没变色了。

self.sheet.write_string(self.row, col, val)

改成

self.sheet.write_string(self.row, col, val, style) 
qq_37934574
qq_37934574 非常感谢,已经可以了
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!