python - 从excel运行宏时xlwings keyerror
问题描述
我正在关注 xlwings 教程。我下载了 xlwings 插件,并使用了命令
xlwings quickstart heart
它创建了一个包含两个文件 heart.xlsm 和 heart.py 的文件夹。然后我检查了excel文件中的宏,并且有一个宏具有以下内容:
Sub SampleCall()
mymodule = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
RunPython ("import " & mymodule & ";" & mymodule & ".main()")
End Sub
我想我确实完全按照教程进行了操作,并且确保安装了 pywin 依赖项
完整的错误消息如下:
---------------------------
Error
---------------------------
Traceback (most recent call last):
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 466, in __call__
return Book(xl=self.xl(name_or_index))
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 156, in __call__
v = self._inner(*args, **kwargs)
File "C:\Users\ETHAN-~1\AppData\Local\Temp\gen_py\3.8\00020813-0000-0000-C000-000000000046x0x1x9.py", line 41312, in __call__
ret = self._oleobj_.InvokeTypes(0, LCID, 2, (13, 0), ((12, 1),),Index
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\ethan-tuna\documents\excel plugin experiments\openpyxl test\heart\heart.py", line 6, in main
wb = xw.Book.caller()
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 613, in caller
return cls(impl=app.books[name].impl)
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 80, in __getitem__
return self(key)
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 51, in __call__
return self._wrap(impl=self.impl(name_or_index))
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 468, in __call__
raise KeyError(name_or_index)
KeyError: 'c:\\users\\ethan-tuna\\documents\\excel plugin experiments\\openpyxl test\\heart\\heart.xlsm'
Press Ctrl+C to copy this message to the clipboard.
---------------------------
OK
---------------------------
根据建议,我删除了文件夹 %localappdata%\Temp\gen_py\3.8,然后我收到以下错误消息
---------------------------
Error
---------------------------
Traceback (most recent call last):
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 466, in __call__
return Book(xl=self.xl(name_or_index))
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 156, in __call__
v = self._inner(*args, **kwargs)
File "C:\Users\ETHAN-~1\AppData\Local\Temp\gen_py\3.8\00020813-0000-0000-C000-000000000046x0x1x9.py", line 41312, in __call__
ret = self._oleobj_.InvokeTypes(0, LCID, 2, (13, 0), ((12, 1),),Index
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\ethan-tuna\documents\excel plugin experiments\openpyxl test\heart\heart.py", line 6, in main
wb = xw.Book.caller()
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 613, in caller
return cls(impl=app.books[name].impl)
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 80, in __getitem__
return self(key)
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\main.py", line 51, in __call__
return self._wrap(impl=self.impl(name_or_index))
File "C:\Users\Ethan-tuna\AppData\Roaming\Python\Python38\site-packages\xlwings\_xlwindows.py", line 468, in __call__
raise KeyError(name_or_index)
KeyError: 'c:\\users\\ethan-tuna\\documents\\excel plugin experiments\\openpyxl test\\heart\\heart.xlsm'
Press Ctrl+C to copy this message to the clipboard.
---------------------------
OK
---------------------------
解决方案
推荐阅读
- c# - TikaOnDotNet - 无法提取文本文件内容
- javascript - 在同一画布上绘制两个图像并使其在角度应用程序中响应
- javascript - discord.js(12) 命令权限问题
- java - javax 无法将套接字转换为 TLS;
- serialization - Godot/Gdscript:为什么 str2var 不能使用 _init 方法处理类?
- python - 如何找到两个熊猫数据框之间的交集
- javascript - 可以在 sys_properties 中使用占位符吗?
- angular - 如何封装组件(工具栏)内容以防止组件(工具栏)成为单体?
- sql - 从 Hangfire.Job 表中获取参数
- javascript - 在路由器中使用带有单独路由的上下文时,反应组件无限期地重新渲染