gpt4 book ai didi

python - 从 Python 中断在 Excel 中创建新工作簿

转载 作者:太空宇宙 更新时间:2023-11-04 11:04:14 25 4
gpt4 key购买 nike

我正在尝试使用标准的 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.....

祝你好运!!

关于python - 从 Python 中断在 Excel 中创建新工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2831491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com