我正在尝试使用标准的 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 出了问题?
您可能想在 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.....
祝你好运!!
本
我是一名优秀的程序员,十分优秀!