1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python xlwt 解决报错:ValueError: More than 4094 XFs (styles)

python xlwt 解决报错:ValueError: More than 4094 XFs (styles)

时间:2018-11-29 07:36:50

相关推荐

python xlwt 解决报错:ValueError: More than 4094 XFs (styles)

解决方案

对于设置样式的函数:

def define_style():font = xlwt.Font()font.colour_index = 1# 初始化样式my_style = xlwt.XFStyle()my_style.font = font # 设置字体return my_style

写入数据时,不要使用sheet.write(0, 0, '数据', define_style())这种方式,改为:

mystyle = define_style() # 把样式函数赋值sheet.write(0, 0, '数据', mystyle) # 在写入数据时不调用函数,而是传入值

例子分析

如下代码会报错

import xlwtdef define_style():font = xlwt.Font()font.colour_index = 1# 初始化样式my_style = xlwt.XFStyle()my_style.font = font # 设置字体return my_styleif __name__ == '__main__':book = xlwt.Workbook(encoding='utf-8')sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)# mystyle = define_style()for i in range(10000):sheet.write(i, 0, u'(0,0)', define_style()) # 注意这里的 define_style() !!!!!!!book.save('my_excel.xlsx')

修改main函数里的局部为:

mystyle = define_style()for i in range(10000):sheet.write(i, 0, u'(0,0)', mystyle) # 这里使用 mystyle

原因解析

在使用xlwt写入数据时,在传入XFStyle格式的样式时,不要使用匿名函数调用写入,否则写入4094个数据后就会超过阈值,报错:ValueError: More than 4094 XFs (styles)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。