Problems Accessing Ms Word 2010 With Python
Solution 1:
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.
Solution 2:
The win32 api for calling system api's is great and all but it is a chore.
If you're open for the idea and you know you'll be accessing the newer document format by windows (based on XML), that is .docx
i'd suggest using a native module such as python-docx.
There's no reason for using the pyWin32 module unless you're going to some very specific tasks.
There's also alternatives for Excel, such as openpyxl
As to your original problem, i'm guessing that the Word
you're hooking against is not actually Microsft Word 2013
but rather an unknown or missing application.
Quote Link (This describes youre issue and validates my guess that Word.Application
is not actually an application)
You are trying to use a ProgID that does not exist. A "ProgID" is really just a mapping to its CLSID. It sounds like your object is not registering itself correctly.
Look in the registry - all COM objects have their name directly under HKEY_CLASSES_ROOT. Under that name, you will find a CLSID. This CLSID will then have a key under HKEY_CLASSES_ROOT\CLSID. Look at the registry to confirm that the names you tried do not exist as COM objects.
Otherwise, try using the CLSID of the object directly, instead of the ProgID - just pass the IID string directly to Dispatch()
I checked my registry under HKEY_CLASSES_ROOT\CLSID\
and searched for Word standing on that Key (folder). I got:
Key: {00020-0000-0000-0000-00000-0000}
titled: Microsoft Word Document
with a sub-folder called ProgID
, with the value: Word.Document.8
Which would let me to do:
import win32com.client as win32
word = win32.Dispatch("Word.Document.8")
word.Visible = 0
word.Documents.Open("myfile.docx")
doc = word.ActiveDocument
print doc.Content.Text
word.Quit()
Now, this is an older version of Word, since i don't have Word 2013 or even something fancy as 2010 :) Or i could just enter the KEY which would be 00020-000....
(i think).
A neat lazy-mans workaround Video tutorial here:
Post a Comment for "Problems Accessing Ms Word 2010 With Python"