我正在尝试使用股票标准win32com方法从Python驱动Excel 2007。但是,当我尝试创建一个新工作簿时,事情就变成了梨形:
Python 2.6.4 (r264:75706, Nov 3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32
...
>>> import win32com.client
>>> excel = win32com.client.Dispatch("Excel.Application")
>>> wb = excel.Workbooks.Add()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
wb = excel.Workbooks.Add()
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 467, in __getattr__
if self._olerepr_.mapFuncs.has_key(attr): return self._make_method_(attr)
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 295, in _make_method_
methodCodeList = self._olerepr_.MakeFuncMethod(self._olerepr_.mapFuncs[name], methodName,0)
File "C:\Python26\lib\site-packages\win32com\client\build.py", line 297, in MakeFuncMethod
return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
File "C:\Python26\lib\site-packages\win32com\client\build.py", line 318, in MakeDispatchFuncMethod
s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
File "C:\Python26\lib\site-packages\win32com\client\build.py", line 604, in BuildCallList
argName = MakePublicAttributeName(argName)
File "C:\Python26\lib\site-packages\win32com\client\build.py", line 542, in MakePublicAttributeName
return filter( lambda char: char in valid_identifier_chars, className)
File "C:\Python26\lib\site-packages\win32com\client\build.py", line 542, in <lambda>
return filter( lambda char: char in valid_identifier_chars, className)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 52: ordinal not in range(128)
>>>
这里出了什么问题?我做过傻事,还是Python / win32com / Excel不知怎么坏了?
答案 0 :(得分:3)
您可能希望在http://www.python-excel.org/
查看优秀的xl * t包创建工作簿/工作表非常简单:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save('example.xls')
你不必费心去win32com api .....
祝你好运!本
答案 1 :(得分:1)
我在2.6.3,所以我无法直接检查,但看起来你className
中有一个非ASCII字符,valid_identifier_chars
是一个字节串,所以这打破了它。
关于要检查的事情的几点想法:
如果您确实拥有最新版本的win32com,那么尝试编写build.py并将valid_identifier_chars
更改为Unicode字符串会很麻烦。