Python xlwt : More than 4094 XFs (styles) 解决方法
对于大量数据写入excel文件,如果使用了表格样式而且在循环中定义了样式,就是产生了easyxf对象,那么最多只能新建4094个对象,
然后会抛出 More than 4094 XFs (styles)的异常,对于这种情况,简单的解决方案是把样式定义在循环之外,比如:
# -*- coding: utf-8 -*- from xlrd import open_workbook from xlwt import Workbook, easyxf def 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 i raise if (i + 1) % 1000 == 0: book.save('test.xls') book.sace('test.xls') get_flush()
这样的代码,会抛出异常信息.
改成这样的:
# -*- coding: utf-8 -*- from xlrd import open_workbook from xlwt import Workbook, easyxf def 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 work for i in xrange(10000): try: sheet.write(i,1,u'测试' , default) except: print i raise if (i + 1) % 1000 == 0: book.save('test.xls') book.save('test.xls') get_flush()
就不用担心异常了.