【问题】
Python3中,使用xlwt3时,出现错误:
Traceback (most recent call last): File "G:\34563264_data_from_site\34563264_data_from_site.py", line 283, in <module> main(); File "G:\34563264_data_from_site\34563264_data_from_site.py", line 262, in main wb.save(excelFilename); File "E:\dev_install_root\Python32\lib\site-packages\xlwt3\Workbook.py", line 633, in save doc.save(filename, self.get_biff_data()) File "E:\dev_install_root\Python32\lib\site-packages\xlwt3\Workbook.py", line 583, in get_biff_data before += self.__write_access_rec() File "E:\dev_install_root\Python32\lib\site-packages\xlwt3\Workbook.py", line 436, in __write_access_rec return BIFFRecords.WriteAccessRecord(self.__owner).get() File "E:\dev_install_root\Python32\lib\site-packages\xlwt3\BIFFRecords.py", line 233, in __init__ uowner, b' '*(0x70 - uowner_len)) # (to_py3): added b'...' struct.error: argument for 's' must be a bytes object
【解决过程】
1.参考:
python32版本,出现问题“struct.error: argument for ‘s’ must be a bytes object”的解决办法
去找到出错位置:
Python32\Lib\site-packages\xlwt3\BIFFRecords.py
把WriteAccessRecord -> __init__
中的,pack中的uowner改为uowner.encode("utf-8"),即:
把:
self._rec_data = pack('%ds%ds' % (uowner_len, 0x70 - uowner_len), uowner, b' '*(0x70 - uowner_len)) # (to_py3): added b'...'
改为:
self._rec_data = pack('%ds%ds' % (uowner_len, 0x70 - uowner_len), uowner.encode("utf-8"), b' '*(0x70 - uowner_len)) # (to_py3): added b'...'
即可。
【总结】
Python3中的bytes和str,不知道别人用的是否爽,总之我是用着很郁闷,还是没有Python2中str和unicode用着爽。
转载请注明:在路上 » 【已解决】Python3中,使用xlwt3去生成excel文件时出错:struct.error: argument for ‘s’ must be a bytes object