最近换了个硬盘,重装电脑后,想用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
-
打开记事本,把下列注册表代码复制到记事本中。
-
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] @="