Win32com not dispatching excel, saying 'Server execution failed' - python

My initial script is:
import win32com.client
import os
file1= r'C:\\Users\cevans\Desktop\models1\file.xlsm'
def refresher():
if os.path.exists(file1):
xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=file1,ReadOnly=1)
xl.Visible = True
xl.Application.Run('Report_Tablerefresh1')
xl.Workbooks(1).Close(SaveChanges=1)
xl.Application.Quit()
del xl
refresher()
I am getting this error when trying to do a simple dispatch of excel, oddly the script had been running fine for a few weeks then this error popped up. Any suggestions?
Traceback (most recent call last):
File "C:/Users/cevans/PycharmProjects/RigLocatorMapPull/Pipe_StorageRefresh.py", line 17, in <module>
refresher()
File "C:/Users/cevans/PycharmProjects/RigLocatorMapPull/Pipe_StorageRefresh.py", l ine 10, in refresher
xl = win32com.client.Dispatch("Excel.Application")
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 104, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)

One Solution was to reboot windows OS (doing complete reinstall) as there appeared to be an error with the windows OS registry with excel. I found this to be the cause of other 'Server Execution Failed' type errors with using win32com.

Related

pywintypes.com_error: (-2147418111, 'Call was rejected by callee.', None, None) while running with task scheduler on a server

I'm attempting to use Python to convert xlsx files to PNGs.
I created an executable file and am attempting to run it from the server. Every time I try to manually run the exe, everything goes smoothly. But the exe throws the following error whenever I try to run this task from the task scheduler.
import cv2
import os
import base64
import excel2img
import time
def create_img(excel_path, root_loc,file_first):
# workbook = Workbook(excel_path)
wb_file_name = excel_path
os.chdir('../')
f_first = file_first
img_folder = os.path.join(root_loc, 'images')
print(img_folder)
img_path = os.path.join(img_folder, f'{f_first}.png')
print(img_path)
time.sleep(5)
excel2img.export_img(excel_path, img_path, "", "Sheet1!B2:L47")
The error:
Traceback (most recent call last):
File "excel2img\excel2img.py", line 120, in export_img
File "<COMObject Range>", line 2, in CopyPicture
File "win32com\client\dynamic.py", line 368, in _ApplyTypes_
pywintypes.com_error: (-2147418111, 'Call was rejected by callee.', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 748, in <module>
File "xl_to_img2.py", line 23, in create_img
File "excel2img\excel2img.py", line 84, in export_img
File "excel2img\excel2img.py", line 39, in __exit__
File "excel2img\excel2img.py", line 64, in close
File "<COMObject Open>", line 2, in Close
pywintypes.com_error: (-2147418111, 'Call was rejected by callee.', None, None)
How to fix it?

win32com.client.Dispatch("Photoshop.Application") not working. win32com doesn't detect the installed Photoshop when it runs

When I try to run a script that opens up Adobe Photoshop 2022, I'm met with following errors:
Traceback (most recent call last):
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\XXXXX\XXXXX\XXXXX\XXXXX\Python\Autophoto\Autophoto.py", line 39, in <module>
psApp = win32com.client.Dispatch("Photoshop.Application")
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
IDispatch, None, clsctx, pythoncom.IID_IDispatch
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
The win32com.client.Dispatch("Photoshop.Application") call used to work when an older version of Photoshop was installed, but since I've updated it to the latest version, the code no longer seems to work.
I've tried using the win32com tool to list all COM interfaces but this program seems to crash after a few seconds every time.
Any tips?
(The code is accessible via following link: https://github.com/samualon/Autophoto)

invalid class string using win32com library python

I wrote the flowing to dispatch an excel application. I used the win32 library in python and wrote this:
import win32com.client as win32
import pywinauto
excel = win32.gencache.EnsureDispatch('Excel.Application'
Because of this line i got this error:
K:\venv\Scripts\python.exe K:/test_results.efrat.py
Traceback (most recent call last):
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
win32.types.com_error: (-2147221005, 'Invalid class string', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "K:/test_results.efrat.py", line 8, in <module>
excel = win32.gencache.EnsureDispatch('Excel.Application')
File "K:\venv\lib\site-packages\win32com\client\gencache.py", line 527, in EnsureDispatch
disp = win32com.client.Dispatch(prog_id)
File "K:\venv\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
win32.types.com_error: (-2147221005, 'Invalid class string', None, None)
Process finished with exit code 1
Does anyone know what could be the problem? I have pywin32 and pypiwin32 installed in my library packages.
It works fine for me. Remove your pywin32 from site-packages and try installing the pywin32.exe from the following URL :
https://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/

Error message trying to execute AutoIT script in Python

Hello I'm trying to automate a file drop process and here's my Python script, with AutoIT imported via the win32com module
from selenium import webdriver
import win32com.client
driver = webdriver.Chrome()
driver.get("https://tinyjpg.com/")
assert "TinyJPG" in driver.title
droplocation = driver.find_element_by_class_name("icon")
droplocation.click()
autoit = win32com.client.Dispatch("AutoItX3.Control")
autoit.Run(r"filedrop.exe")
However, I got this error:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Tea\Desktop\compressimg.py", line 30, in <module>
autoit = win32com.client.Dispatch("AutoItX3.Control")
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221164, 'Class not registered', None, None)
The most similar post I could find was this: win32com (python) error, but I think websites are supposed to be COM servers so that's not really relevant here?
In any case, I'm not too sure what the error is trying to tell me, help is appreciated!

Python / ADOX: 'The specified module could not be found.' (win32 extensions)

I'm running pywin32 for python 2.5.
I'm following the instructions for python ADO given at http://www.ecp.cc/pyado.html.
Creating an ADODB.Recordset object works fine. But when I try to create an ADOX.Catalog object I get an error:
>>> cat=win32com.client.Dispatch(r'ADOX.Catalog')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 78, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147024770, 'The specified module could not be found.', None, None)
Any ideas what I might be missing?
Solution: even though ADOX was showing up in the COM browser as an available library, it wasn't "registered" properly. Following the instructions here, I executed the following at the Start->Run prompt:
regsvr32 "C:\Program Files\Common Files\System\ado\msadox.dll"
Note that this is on a WinXP SP2 machine. I guess the registry had become corrupt somehow? In any case, I'm new to Windows programming so my explanation may be off, but maybe the fix will help someone.

Categories

Resources