[python-win32] COM automation: problem accessing property of derived object obtained from collection

Albert Strasheim fullung at gmail.com
Fri Jan 4 08:41:18 CET 2008


Hello all

I'm new to the world of COM automation with Python, so please bear with me.

I'm trying to automate Golden Software's Surfer 8 [1] using Python.

The basics seems to be working fine, but I'm running into a problem when 
dealing with a collection containing instances of an object derived from a 
base object.

The code looks as follows. Judging from the VBScript examples included with 
Surfer, it should work.

from win32com.client import constants, Dispatch
srf = Dispatch("Surfer.Application")
plot = srf.Documents.Add(constants.srfDocPlot)
mapframe = plot.Shapes.AddContourMap(GridFileName=outGrid)
contourmap = mapframe .Overlays.Item(1)
# the next line breaks
print contourMap.Levels

contourmap should contain a Surfer.IContourMap object, which is derived from 
the Surfer.IShape object. I can see in the GUI that the contour map is 
created, so I'm pretty sure everything is working up to this point.

Now, IContourMap has a Levels property that IShape doesn't have. When I try 
to access this property, I get the following exception:

Traceback (most recent call last):
  File "surfer.py", line 7, in <module>
    print contourMap.Levels
  File "C:\Python25\Lib\site-packages\win32com\client\__init__.py", line 
454, in __getattr__
    raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), 
attr)
AttributeError: '<win32com.gen_py.Surfer 8 Type Library.IShape instance at 
0x22544016>' object has no attribute 'Levels'

For some reason Python can't figure out that the IShape is also a 
IContourMap.

As I said, I'm new to COM, so I'm at a loss as to what's going on here? Any 
help would be appreciated.

Cheers,

Albert

[1] http://www.goldensoftware.com/products/surfer/surfer.shtml 



More information about the python-win32 mailing list