Windows 11远程管理中使用xlwings打开Excel文件报错问题
问题描述
运行以下xlwings代码:
wb = xw.Book(r'C:/Users/myUser/Documents/scripts/Analyzer.xlsm')
- Windows 10系统:本地PowerShell终端、Windows远程管理执行均正常
- Windows 11系统:本地执行无报错,远程管理执行时抛出如下错误:
Traceback (most recent call last): File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 711, in __call__ return Book(xl=self.xl(name_or_index)) File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 219, in __call__ v = self._inner(*args, **kwargs) File "C:\Users\myUser\AppData\Local\Temp\gen_py\3.9\00020813-0000-0000-C000-000000000046x0x1x9.py", line 41316, 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 "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\main.py", line 4963, in open impl = self.impl(name) File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 713, in __call__ raise KeyError(name_or_index) KeyError: 'Analyzer.xlsm' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\myUser\Documents\script\macro_new.py", line 92, in wb = xw.Book(r'C:/Users/myUser/Documents/scripts/Analyzer.xlsm') File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\main.py", line 898, in __init__ impl = app.books.open( File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\main.py", line 4970, in open impl = self.impl.open( File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 745, in open xl=self.xl.Open( File "C:\Users\myUser\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 105, in __call__ v = self.__method(*args, **kwargs) File "C:\Users\myUser\AppData\Local\Temp\gen_py\3.9\00020813-0000-0000-C000-000000000046x0x1x9.py", line 41193, in Open ret = self._oleobj_.InvokeTypes(1923, LCID, 1, (13, 0), ((8, 1), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Filename pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Microsoft Excel cannot access the file 'C:\\Users\\myUser\\Documents\\script\\Analyzer.xlsm'. There are several possible reasons:\n\n� The file name or path does not exist.\n� The file is being used by another program.\n� The workbook you are trying to save has the same name as a currently open workbook.", 'xlmain11.chm', 0, -2146827284), None)
已升级xlwings至0.30.4,Python版本3.9.13,问题未解决。
解决方案
1. 修复远程会话的桌面目录缺失问题
Windows 11远程会话默认没有系统级桌面目录,Excel启动时依赖该目录,手动创建并设置权限:
- 新建路径:
C:\Windows\System32\config\systemprofile\Desktop - 右键该目录→属性→安全,给执行脚本的用户添加完全控制权限
2. 配置组策略允许远程桌面交互
- 运行
gpedit.msc打开组策略编辑器 - 导航到:计算机配置>管理模板>Windows组件>远程桌面服务>远程桌面会话主机>设备和资源重定向
- 启用以下两项:
- 允许音频和视频播放重定向
- 允许剪贴板重定向
- 找到不允许远程桌面服务会话使用远程桌面连接,设置为禁用
- 重启远程机器或执行
gpupdate /force生效
3. 修改xlwings代码,显式控制Excel实例
强制以可见模式启动Excel,避免无界面模式的COM权限问题:
import xlwings as xw # 显式创建Excel应用实例,设置可见并禁用弹窗 app = xw.App(visible=True, add_book=False) app.display_alerts = False app.screen_updating = False try: # 打开目标文件 wb = app.books.open(r'C:/Users/myUser/Documents/scripts/Analyzer.xlsm') # 执行你的业务逻辑... finally: # 确保资源释放 wb.close() app.quit()
4. 验证文件路径与权限
- 确认远程执行用户对
C:/Users/myUser/Documents/scripts/目录有读写权限 - 避免路径包含中文、空格或特殊字符,统一使用原始字符串
r''包裹路径
内容的提问来源于stack exchange,提问作者syx007
相关产品推荐
相关产品推荐

