python win32com Dispatch, DispatchEx 无法打开(启动)excel pywintypes.com_error: (-2146959355, ‘服务器运行失败‘

重装系统后,使用Python的win32com尝试打开Excel时遇到com_error。尝试了权限设置和修复注册表项未果。最终通过修改注册表在DCOM配置中重建Excel应用程序,具体步骤包括编辑并执行注册表脚本,替换Excel路径,导入注册表信息,重启电脑后问题解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近换了个硬盘,重装电脑后,想用python打开excel,发现完全没反应,弹出以下错误信息:

asctime:        Sun, 02 May 2021 12:48:36 
bug_line:       line:509 
level:          ERROR 
message:        ERROR:(-2146959355, '服务器运行失败', None, None)
Traceback (most recent call last):
  File "D:\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, '操作无法使用', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/发送房租软件/begin.py", line 503, in open_excel
    send_info_dict,fangzi_shuidian=open_excel.get_send_info()
  File "F:/发送房租软件/begin.py", line 184, in get_send_info
    month_info,shuidian_dict=self.get_all_info()  #获取所有租户所选月份对应的所有具体租金、水电量
  File "F:/发送房租软件/begin.py", line 177, in get_all_info
    self.creat_next_month(file)#提前创建当前月份/下个月的数据
  File "F:/发送房租软件/begin.py", line 215, in creat_next_month
    excel = Dispatch('excel.application')  # 创建Excel对象
  File "D:\Python\Python38\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "D:\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "D:\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2146959355, '服务器运行失败', None, None)

期间下载了wps,以为是这个问题,不断重装excel跟wps,发现还是不行!!

百度了一番,基本是2个说法 

1.权限问题,python跟excel不是同一个权限------------------------------试来试去,都是用管理员授权了,还是不行

2.Excel.Application注册信息不对,所以pywin32无法调用

针对注册的说法,可以参考下这个老哥https://blog.csdn.net/l490134872/article/details/80250395

思路是对了,不过我在注册表查询时,发现有好几个Microsoft Excel Application,程序ID也是对得上的(注意在注册表搜索时,它的匹配方式是匹配!

所以得把它全改了才行!!!

太费劲了,所以我想,要不直接 通过注册表在dcom配置中重建excel应用程序 !!

参考这篇文章,具体内容我也贴上来了https://jingyan.baidu.com/article/14bd256e79a67dbb6d261299.html

  1. 打开记事本,把下列注册表代码复制到记事本中。

  2. Windows Registry Editor Version 5.00
    
    
    [HKEY_CLASSES_ROOT\AppID\{00020820-0000-0000-C000-000000000046}]
    
    @="Microsoft Office Excel 97-2003 工作表"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}]
    
    "IPersistStorageType"=dword:00000002
    
    @="Microsoft Office Excel 97-2003 工作表"
    
    "AppID"="{00020820-0000-0000-C000-000000000046}"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\AuxUserType]
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\AuxUserType\2]
    
    @="Worksheet"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\AuxUserType\3]
    
    @="Microsoft Office Excel 2003"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\Conversion]
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\Conversion\Readable]
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\Conversion\Readable\Main]
    
    @="Biff8,ExcelWorksheet,ExcelML12,Biff12"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\Conversion\Readwritable]
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\Conversion\Readwritable\Main]
    
    @="Biff8,ExcelML12,Biff12"
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\DataFormats]
    
    
    [HKEY_CLASSES_ROOT\CLSID\{00020820-0000-0000-C000-000000000046}\DataFormats\DefaultFile]
    
    @="
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值