# 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.
Related
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?
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)
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
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/
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!