用xlutils.copy写入中文的问题
阅读原文时间:2023年07月08日阅读:1

用xlutils.copy 将中文写入excel文档中

遇到的问题1:

Traceback:
Traceback (most recent call last):
File "C:\Users\Druid\Desktop\New Folder (3)\Luminos RF I.I. system\test_xlwt.py", line 24, in 
wb.save(os.path.join(filepath,out_name))
File "D:\python\python27\lib\site-packages\xlwt\Workbook.py", line 643, in save
doc.save(filename, self.get_biff_data())
File "D:\python\python27\lib\site-packages\xlwt\Workbook.py", line 611, in get_biff_data
before += self.all_fonts_num_formats_xf_styles_rec()
File "D:\python\python27\lib\site-packages\xlwt\Workbook.py", line 516, in __all_fonts_num_formats_xf_styles_rec
return self.__styles.get_biff_data()
File "D:\python\python27\lib\site-packages\xlwt\Style.py", line 183, in get_biff_data
result += self._all_num_formats()
File "D:\python\python27\lib\site-packages\xlwt\Style.py", line 208, in _all_num_formats
result += NumberFormatRecord(fmtidx, fmtstr).get()
File "D:\python\python27\lib\site-packages\xlwt\BIFFRecords.py", line 791, in __init
ufmtstr = upack2(fmtstr)
File "D:\python\python27\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
us = unicode(s, encoding)
TypeError: coercing to Unicode: need string or buffer, NoneType found

用xlutils写文件的时候报这个错了。

需要把下面这部分改成下面这样才好使。

start from line45)
def upack2(s, encoding='ascii'):
# If not unicode, make it so.
if isinstance(s, unicode):
us = s
elif s is not None:
us = unicode(s, encoding)
else:
us = unicode('', encoding)

遇到的问题2:

写入中文,需要将

Workbook.py中的初始化改为utf8,原来默认的是ascii

def __init__(self, encoding='utf8', style_compression=0):