You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xlwings RPC服务器不可用问题求助:Win11特定文件集报错

Xlwings在Windows 11下RPC连接丢失问题的排查请求

问题概述

使用Xlwings处理Excel文件时,特定文件集执行master_wb.save()时出现RPC连接丢失,报错信息为:
(-2147023174, 'The RPC server is unavailable.', None, None)
后续还会抛出:
pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

系统环境

  • 操作系统:桌面版Windows 11
  • Python版本:3.12.2
  • Pandas版本:2.2.1
  • Xlwings版本:0.31.1

脚本逻辑与核心代码

脚本需处理a_master.xlsx及a_1.xlsx至a_n.xlsx文件集,逻辑为打开主文件后遍历其他子文件,通过Xlwings调用宏更新主文件数据。核心代码如下:

import xlwings as xw
import os  # 原代码未导入os,此处补充以保证代码可运行

shtname = 'a sheet name'

app = xw.app(visible=visibleflag)

master_wb = xw.Book('a_master.xlsx', update_links=False)

if not master_wb.sheets.active.name == shtname:     # 确保切换到目标工作表
    master_wb.sheets[shtname].activate()  # 原代码为activeate(),推测是笔误,此处修正为activate()

nrows, m_hdrs, nsheets = gethdrs('a_master.xlsx', shtname) 
update_master = master_wb.app.macro('macroname')

for file in os.listdir('.'):
    if file.endswith(".xlsx") and file.find("~$") < 0:
        if file != 'a_master.xlsx':
           nrows, f_hdrs, nsheets = gethdrs(file, shtname) # 依赖openpyxl实现

           # 校验表头一致性逻辑...
           filewb = xw.Book(file, read_only=True, update_links=False, ignore_read_only_recommended=True)
           if not filewb.sheets.active.name == shtname: # 确保切换到目标工作表
               filewb.sheets[shtname].activate()
           update_master('a_master.xml', file, startCol, rowcnt, debug) # 调用宏更新数据
           try:
               filewb.close()       # 此处执行会抛出异常,暂时用try-except跳过
           except:
               pass

master_wb.save()
master_wb.close()
app.quit()       

报错具体现象

仅特定一组文件触发问题:

  • 设置visible=True时,可见Excel尝试保存主文件后无响应,弹出带十六进制文件名的保存对话框
  • 修改文件名会提示共享冲突,保存后触发RPC错误,且Excel会重新打开该十六进制文件名的文件
  • 该文件集曾使用全新数据时正常运行,但再次执行时复现问题;其他文件集总大小、行数相近,无此问题

已尝试的排查方法

  • 使用Excel Inquire检测报错文件集,未发现异常
  • 升级/降级Xlwings版本,问题未解决
  • 在Win10机器上运行脚本无异常;搭建Win10环境测试该报错文件集,运行正常

请求进一步排查思路

希望能得到针对Windows 11环境下该问题的排查方向或解决方案。

内容的提问来源于stack exchange,提问作者tank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:05:07