[python-win32] Invoking methods from different COM interfaces

Matthew Woodcraft mjw@pearson.co.uk
Fri, 15 Feb 2002 18:29:17 -0000


I have used makepy to build a type library for 'Microsoft XML, v3.0
(3.0)'. Now I've done this, I find that some of the DOM methods, eg
'createNode', return values of type
<win32com.gen_py.Microsoft XML, v3.0.IXMLDOMNode>.

In some cases, I know that the COM object returned also supports the
IXMLDOMElement interface - but Python raises an exception if I try to
invoke methods of this interface directly.

Is there a way to 'cast' the object to one representing an
IXMLDOMElement, or otherwise invoke the methods of IXMLDOMElement, from
Python?

If I don't build the Python type library, the objects I get from methods
like createNode have types like <COMObject createNode>, and happily let
me invoke methods of both IXMLDOMNode and IXMLDOMElement.


Example:

import win32com.client
doc = win32com.client.Dispatch("Msxml2.DOMDocument")
NODE_ELEMENT = 1
NAMESPACE_URI = "http://foo.invalid/"
el_a = doc.createNode(NODE_ELEMENT, "el-a", NAMESPACE_URI)
print el_a.tagName


Without building a type library, this works. With a type library, I get
AttributeError for the last line.

In most cases, I can work around this. But it seems like there ought to
be a way to choose from the different interfaces supported by a COM
object. What am I missing?

I am using Python 2.1 from PythonLabs, and the corresponding win32all
supplied by ActiveState.

-M-