invalid class string using win32com library python - 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/

Related

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)

Conversion of .doc to .docx

# import necessary packages
from win32com import client
def doc2docx(doc_name):
word = client.Dispatch("Word.application")
doc = word.Documents.Open(doc_name)
docx_name = doc_name.replace(".doc", ".docx")
doc.SaveAs(docx_name, 16)
doc.Close()
word.Quit()
if __name__ == "__main__":
doc2docx("./sample_docs/example.doc")
I tried the above code, I'm getting pywintypes.com_error at line
doc = word.Documents.Open(doc_name)
Does anyone know what could be the problem? I have pypiwin32 installed in my library packages. Below is the trackback of the error message.
Traceback (most recent call last):
File "C:\Users\bcp\anaconda3\envs\del\lib\site-packages\win32com\client\dynamic.py", line 89, 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 "redaction_doc.py", line 14, in <module>
doc2docx("./sample_docs/example.doc")
File "redaction_doc.py", line 6, in doc2docx
word = client.Dispatch("Word.application")
File "C:\Users\bcp\anaconda3\envs\del\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Users\bcp\anaconda3\envs\del\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\bcp\anaconda3\envs\del\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
I tried with #dshefman's answer but getting the same error.

Not able to Connect to HP ALM 12.55 using Python's Dispatch("TDApiOle80.TDConnection") Object

Not able to connect to HP ALM through Disppatch method .
Executing the following code:
import win32com.client
qc = win32com.client.Dispatch("TDApiOle80.TDConnection")
Results in the following error on a Windows 64 bit machine with ALM version 12.55.
Traceback (most recent call last): File"C:\Users\User1\AppData\Local\Programs\Python\Python37-32\lib\sit``e-packages\win32com\client\dynamic.py", line 89, 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 "<stdin>", line 1, in <module>File"C:\Users\User1\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\
__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Users\User1\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\
dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\User1\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\
dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
How to solve this?
Make sure you installed correct version of ALM Connectivity tool (TDConnect.exe) from http://<youralmserver:port>/qcbin/PlugIns/TDConnectivity/TDConnect.exe

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!

Win32com not dispatching excel, saying 'Server execution failed'

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.

Categories

Resources