Problems Accessing MS Word 2010 with Python
Question: I am using Python with Eclipse. I need to access MS Word file with Python. I
have seen some examples on this and I have already installed pywin32. I tried
some of the examples but I am getting some errors.

    
    
    import win32com.client as win32
    
    word = win32.Dispatch("Word.Application")
    word.Visible = 0
    word.Documents.Open("myfile.docx")
    doc = word.ActiveDocument
    print doc.Content.Text
    word.Quit()
    

This is the error I am getting. It would be great if anyone can tell me what I
did wrong here.

    
    
    Traceback (most recent call last):
      File "C:\Users\dino\Desktop\Python27\Test\src\AccessWordDoc.py", line 10, in <module>
        word = win32.Dispatch("Word.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 114, in _GetGoodDispatchAndUserName
        return (_GetGoodDispatch(IDispatch, clsctx), userName)
      File "C:\Python27\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)
    

Is there another way to access the MS word file and extract the data in it
without going through all this?


Answer: The code below worked for me, which is just a simple change of
"Word.Application" to "Word.Application.8":

    
    
    import win32com.client as win32
    
    word = win32.Dispatch("Word.Application.8")
    word.Visible = 0
    word.Documents.Open("myfile.docx")
    doc = word.ActiveDocument
    print doc.Content.Text
    word.Quit()
    

I came to this solution following @Torxed's suggestion to examine the
registry. When I tried Word.Document.8, the set of methods available did not
include .Visible, .Quit, and .Open and so @Torxed's solution did not work for
me. (It is clear now that the Application and Word objects are intended to
have different uses.) Instead, I also found Word.Application,
Word.Application.8, and Word.Application.14 under my registry and just tried
Word.Application.8 and it worked as expected.

