1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【Python】Python xlwt : More than 4094 XFs (styles) 解决方法

【Python】Python xlwt : More than 4094 XFs (styles) 解决方法

时间:2020-04-29 18:27:15

相关推荐

【Python】Python xlwt : More than 4094 XFs (styles) 解决方法

对于大量数据写入excel文件,如果使用了表格样式而且在循环中定义了样式,就是产生了easyxf对象,那么最多只能新建4094个对象,

然后会抛出More than 4094 XFs (styles)的异常,对于这种情况,简单的解决方案是把样式定义在循环之外,比如:

# -*- coding: utf-8 -*-from xlrd import open_workbookfrom xlwt import Workbook, easyxfdef get_flush():book = Workbook()sheet = book.add_sheet('test')print 'flush_row_data' in dir(sheet)## default = easyxf('font: name Arial;')for i in xrange(10000):try:sheet.write(i,1,u'测试' ,easyxf('font: name Arial;'))except:print iraiseif (i + 1) % 1000 == 0:book.save('test.xls')book.sace('test.xls')get_flush()

这样的代码,会抛出异常信息.

改成这样的:

# -*- coding: utf-8 -*-from xlrd import open_workbookfrom xlwt import Workbook, easyxfdef get_flush():book = Workbook()sheet = book.add_sheet('test')print 'flush_row_data' in dir(sheet)default = easyxf('font: name Arial;') # define style out the loop will workfor i in xrange(10000):try:sheet.write(i,1,u'测试' , default)except:print iraiseif (i + 1) % 1000 == 0:book.save('test.xls')book.save('test.xls')get_flush()

就不用担心异常了.

转自:/jaw-crusher/p/3741224.html(感谢jaw-crusher)

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