在测试 xlwings 控制电子表格时候,碰到拿到了这个问题,详细报错信息:
D:\Anaconda3\envs\yolo\python.exe E:/workspace/OtherProject/ankangdaka/sheet.py Traceback (most recent call last): File "E:\workspace\OtherProject\ankangdaka\sheet.py", line 15, in <module> main() File "E:\workspace\OtherProject\ankangdaka\sheet.py", line 11, in main xw_process(xls_path) File "E:\workspace\OtherProject\ankangdaka\sheet.py", line 5, in xw_process app = xw.App(visible=True, add_book=False) File "D:\Anaconda3\envs\yolo\lib\site-packages\xlwings\main.py", line 294, in __init__ self.impl = engines.active.apps.add( File "D:\Anaconda3\envs\yolo\lib\site-packages\xlwings\main.py", line 203, in add return App(impl=self.impl.add(**kwargs)) File "D:\Anaconda3\envs\yolo\lib\site-packages\xlwings\_xlwindows.py", line 497, in add return App(spec=spec, add_book=add_book, xl=xl, visible=visible) File "D:\Anaconda3\envs\yolo\lib\site-packages\xlwings\_xlwindows.py", line 521, in __init__ self._xl = COMRetryObjectWrapper(DispatchEx("Excel.Application")) File "D:\Anaconda3\envs\yolo\lib\site-packages\win32com\client\__init__.py", line 145, in DispatchEx dispatch = pythoncom.CoCreateInstanceEx( pywintypes.com_error: (-2147221005, '无效的类字符串', None, None) Process finished with exit code 1
可以定位到问题的出错是:DispatchEx("Excel.Application")。因为我这里使用的是wps来测试,电脑上本身是没有安装Excel的,需要更改为:DispatchEx("KET.Application")
如下所示:
随后程序即可执行。
参考资料: