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

RevolverRakk
  • 309
  • 4
  • 10
  • You did not say, and you should have, where the error is reported. Always include *the full stack trace in your question* because that means we don't have to scan through your code to see where the problem might be. But the line that gives that error is probably `client.Dispatch("Word.application")`. The class name in the call probably has a spelling error. – BoarGules Oct 16 '20 at 06:00
  • What is the **_exact_** error message you're getting? We can't help you if you're not more specific. Do not shorten the error message; just show the complete traceback. – pepoluan Oct 16 '20 at 06:00
  • Does this answer your question? [multiple .doc to .docx file conversion using python](https://stackoverflow.com/questions/38468442/multiple-doc-to-docx-file-conversion-using-python) – Vignesh Oct 16 '20 at 06:26
  • @VigneshRajendran thanks, but I'm getting the same error while implementing that. – RevolverRakk Oct 16 '20 at 06:47
  • @BoarGules, could you please explain this a little bit in detail. "The class name in the call probably has a spelling error." – RevolverRakk Oct 16 '20 at 06:53
  • Invalid class string indicates that you do not have microsoft word installed –  Oct 16 '20 at 07:03
  • Spelling error: the class named in the call does not exist. That is either because it is spelled wrong or (if it is correct) because it doesn't exist on your computer. As @JustinEzequiel says, do you have Word installed? All this code does is fire up Word and ask it to do what you want. – BoarGules Oct 16 '20 at 09:22

0 Answers0