# 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.