[python-win32] COM Automation: Problem Accessing Property of Object

Emre CETIN e.cetin at yahoo.com
Tue May 22 16:08:40 EDT 2018


Thank you for your reply. I wanted to try it but for seom reason the code that has been running without a problem suddenly gave this error:

import win32com.client
srf = win32com.client.gencache.EnsureDispatch('Surfer.Application')  # after this line of code the error below is produced...

 ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-5e3c96b21b0e> in <module>()
----> 1 srf = win32com.client.gencache.EnsureDispatch('Surfer.Application')

C:\Program Files\Anaconda3\lib\site-packages\win32com\client\gencache.py in EnsureDispatch(prog_id, bForDemand)
    538                         tlb, index = ti.GetContainingTypeLib()
    539                         tla = tlb.GetLibAttr()
--> 540                         mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
    541                         GetModuleForCLSID(disp_clsid)
    542                         # Get the class from the module.

C:\Program Files\Anaconda3\lib\site-packages\win32com\client\gencache.py in EnsureModule(typelibCLSID, lcid, major, minor, progressInstance, bValidateFile, bForDemand, bBuildHidden)
    395         try:
    396                 try:
--> 397                         module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
    398                 except ImportError:
    399                         # If we get an ImportError

C:\Program Files\Anaconda3\lib\site-packages\win32com\client\gencache.py in GetModuleForTypelib(typelibCLSID, lcid, major, minor)
    264         # module to our cache though - check that here.
    265         if "_in_gencache_" not in mod.__dict__:
--> 266                 AddModuleToCache(typelibCLSID, lcid, major, minor)
    267                 assert "_in_gencache_" in mod.__dict__
    268         return mod

C:\Program Files\Anaconda3\lib\site-packages\win32com\client\gencache.py in AddModuleToCache(typelibclsid, lcid, major, minor, verbose, bFlushNow)
    556         # module - this doesn't mean anything special though!
    557         mod._in_gencache_ = 1
--> 558         dict = mod.CLSIDToClassMap
    559         info = str(typelibclsid), lcid, major, minor
    560         for clsid, cls in dict.items():

AttributeError: module 'win32com.gen_py.54C3F9A2-980B-1068-83F9-0000C02A351Cx0x1x4' has no attribute 'CLSIDToClassMap'

Any idea of what this is and how to solve it?

E m r e
 

    On Tuesday, May 22, 2018, 8:14:34 PM GMT+3, Tim Roberts <timr at probo.com> wrote:  
 
 Emre CETIN via python-win32 wrote:
> Hi, I've been looking for a solution to this question for some time
> now. Hopefully you could help me out. There's a program (Golden
> Software Surfer) that I have successfully automated using Python COM.
> I normally am able to control every portion I need. The part I have
> trouble accessing is under its "Property Manager". Even though I am
> using the description provided in the software's documents & help
> files I get an error saying:
>
> AttributeError: '<win32com.gen_py.Surfer 13 Type Library.IShape
> instance at 0x2327715907232>' object has no attribute 'ShowColorScale'
>
> "ShowColorScale" is just an example of one property/method in the
> Property Manager window.
>
> Here is the example code I have been working with:
> |importwin32com.client srf
> =win32com.client.gencache.EnsureDispatch('Surfer.Application')Plot=srf.Documents.Add(1)srf.Visible=TrueMapFrame1=Plot.Shapes.AddImageMap(GridFileName="C:/test.grd")ImageLayer1=MapFrame1.Overlays(1)ImageLayer1.ShowColorScale=True#
> this is where i get the error|

The problem here, I think, is that ShowColorScale is a property of the
IContourMap interface, but you've been given an IShape interface.  One
of the issues about COM is that some objects implement many different
interfaces, and you have to know which interface to ask for to get the
methods and properties you want.

The answer, as the old poster suggested, is to query the IShape object
for its IContourMap interface.  It shouldn't be very much more
complicated than this:

    ImageMap1 = ImageLayer1.CastTo( "IContourMap" )
    ImageMap1.ShowColorScale = True

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
python-win32 at python.org
https://mail.python.org/mailman/listinfo/python-win32
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20180522/5900bf9f/attachment-0001.html>


More information about the python-win32 mailing list