对于大量数据写入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)