适用于Python3.x的第三方库正在逐渐完善中,使用过程中
当然免不了出现各种问题,本文汇总了我近期使用操作excel的xlwt3包遇到的问题,仅供分享与总结。
1.导入xlwt3报错:ValueError: '__init__' in __slots__ conflicts with class variable
使用pip install xlwt3 或者下载xlwt3 使用python setup.py install安装xlwt3
测试安装
import xlwt3
详细出错信息如下:
Traceback (most recent call last):
File "F:\temp\mycode\frist\frist.py", line 132, in
import xlwt3
File "C:\Python33\lib\site-packages\xlwt3\__init__.py", line 3, in
from .workbook import Workbook
File "C:\Python33\lib\site-packages\xlwt3\workbook.py", line 5, in
from .worksheet import Worksheet
File "C:\Python33\lib\site-packages\xlwt3\worksheet.py", line 7, in
from .row import Row
File "C:\Python33\lib\site-packages\xlwt3\row.py", line 8, in
from . import formula
File "C:\Python33\lib\site-packages\xlwt3\formula.py", line 6, in
class Formula(object):
ValueError: '__init__' in __slots__ conflicts with class variable
解决办法如下:
打开Python33\Lib\site-packages\xlwt3\formula.py文件,将其中的
__slots__ = ["__init__", "__s", "__parser", "__sheet_refs", "__xcall_refs"]
修改为
__slots__ = [ "__s", "__parser", "__sheet_refs", "__xcall_refs"]
注意:修改后测试不成功,多试几次,空格制表换行都有可能导致修改不成功。
2.cannot import BiffRecord 或者ImportError: No module named 'BIFFRecords‘
解决办法:
使用编辑器或者IDLE或Wing IDE,打开“..\Lib\site-packages\xlwt3\BIFFRecords.py”修编码方式为'UTF-8'
3.生成excel文件时出错:
struct.error: argument for ‘s’ must be a bytes object
解决办法:
def __init__(self, owner): uowner = owner[0:0x30] uowner_len = len(uowner) self._rec_data = pack('%ds%ds' % (uowner_len, 0x70 - uowner_len), uowner, b' '*(0x70 - uowner_len)) # (to_py3): added b'...'
修改为
def __init__(self, owner):
uowner = owner[0:0x30]
uowner_len = len(uowner)
self._rec_data = pack('%ds%ds' % (uowner_len, 0x70 - uowner_len),
uowner.encode('utf-8'), b' '*(0x70 - uowner_len)) # (to_py3): added b'...'
原文网址: http://ljchu.blog.163.com/blog/static/213753199201301062134786/