macOS下xlwings调用OneDrive中Excel宏遇EventError(-50)及文件损坏求助
问题:macOS下通过xlwings调用OneDrive中Excel宏失败
环境信息
- macOS Catalina 10.15.7
- PyCharm 2021.2.3
- Python 3.8.8(Anaconda 22.11.1 作为解释器)
- Microsoft Excel for Mac 16.66.1
需求与代码
需通过Python的xlwings包执行OneDrive中[StakeOverflow]HelloWorld.xlsm文件内的Hi宏。
宏代码:
Sub Hi() MsgBox "Good morning!" End Sub
Python代码:
import xlwings as xw import time wb = xw.Book('/Users/<MY NAME>/OneDrive/MY PATH DETAILS/[StakeOverflow]HelloWorld.xlsm') time.sleep(10) app = wb.app macro_vba = app.macro("Hi") macro_vba()
问题现象
- 执行Python代码后,Excel未启动,触发报错:
/Users/.../.conda/envs/Program/bin/python "/Users/.../OneDrive/.../MY_PYTHON_FILE.py" Traceback (most recent call last): File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 4914, in open impl= self.impl(name) File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/_xlmac.py", line 366, in __call__ raise KeyError(name_or_index) KeyError: '[stakeoverflow] helloworld.xlsm' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/appscript/reference.py", line 482, in __call___ return self.AS_appdata.target().event (self._code, params, atts, codecs=self.AS_appdata).send(timeout, sendflags) File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/aem/aemsend.py", line 92, in send raise EventError(errornum, errormsg, eventresult) aem.aemsend.EventError: Command failed: Parameter error. (-50) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/.../OneDrive/.../MY_PYTHON_FILE.py", line 18, in <module> wb = xw.Book('/Users/.../OneDrive/.../[stakeoverflow] helloworld.xlsm') File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 876, in __init__ impl= app.books.open( File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 4921, in open impl = self.impl.open( File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/_xlmac.py", line 420, in open self.app.xl.open_workbook( File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/appscript/reference.py", line 518, in __call__ raise CommandError(self, (args, kargs), e, self.AS_appdata) from e appscript.reference.CommandError: Command failed: OSERROR: -50 MESSAGE: Parameter error. COMMAND: app(pid=1647).open_workbook (workbook_file_name='/users/.../onedrive/.../[stakeoverflow] helloworld.xlsm', update_links=k.do_not_update_links, read_only=None, format=None, password=None, write_reserved_password=None, ignore_read_only_recommended=None, origin=None, delimiter=None, editable=None, notify=None, converter=None, add_to_mru=None, timeout=-1) Process finished with exit code 1
- 手动打开该Excel文件时,提示:
Excel cannot open the file ’[StakeOverflow]HelloWorld.xlsm’ because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
- 文件移至桌面可正常运行,但需解决OneDrive存储场景下的问题。
解决方案
方案1:使用原始字符串路径处理特殊字符
文件名中的方括号可能导致路径解析异常,将Python代码中的路径改为原始字符串:
wb = xw.Book(r'/Users/<MY NAME>/OneDrive/MY PATH DETAILS/[StakeOverflow]HelloWorld.xlsm')
方案2:先启动Excel再连接实例
避免xlwings启动Excel时的权限或路径问题,先手动打开Excel并确保OneDrive文件同步完成,再修改代码:
import xlwings as xw import time # 连接已打开的Excel实例 app = xw.apps.active # 打开指定工作簿 wb = app.books.open(r'/Users/<MY NAME>/OneDrive/MY PATH DETAILS/[StakeOverflow]HelloWorld.xlsm') time.sleep(2) macro_vba = app.macro("Hi") macro_vba()
方案3:检查OneDrive同步与权限
- 确认OneDrive已完成文件同步,无同步报错
- 关闭所有可能占用该文件的程序,避免文件锁定
- 在系统偏好设置>安全性与隐私中,给PyCharm和Excel授予完整磁盘访问权限
方案4:移除文件名中的特殊字符
将文件名修改为不含方括号的格式(如StakeOverflow_HelloWorld.xlsm),同时更新Python代码中的路径,再测试运行。
内容的提问来源于stack exchange,提问作者Datoto
相关产品推荐
相关产品推荐

