qq_37934574 2019-08-23 10:51 采纳率: 20%
浏览 581
已采纳

求大神指点,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条回答 默认 最新

  • FeiRouBing 2019-08-23 13:27
    关注

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

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

    改成

    self.sheet.write_string(self.row, col, val, style) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿