首页 > 解决方案 > 从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   
---------------------------


标签: pythonxlwings

解决方案


推荐阅读