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

Emre CETIN e.cetin at yahoo.com
Tue May 22 16:57:25 EDT 2018


Hello again,
Finally I got it to work! Here it is:
import win32com.client

srf = win32com.client.gencache.EnsureDispatch('Surfer.Application')
Plot = srf.Documents.Add(1)
srf.Visible = True
MapFrame1 = Plot.Shapes.AddImageMap(GridFileName="C:/test.grd")
ImageLayer1 = MapFrame1.Overlays(1)
ImageMap1 = win32com.client.CastTo(ImageLayer1, "IImageLayer")  # the property was under IImageLayer
ImageMap1.ShowColorScale = True

Your help lead me to the solution. I now understand what the issue was and how to use CastTo to get it working.
Thank you and Mr.Hammond so much during for all your help!
Best regards,

E m r e
 

    On Tuesday, May 22, 2018, 11:21:35 PM GMT+3, Emre CETIN <e.cetin at yahoo.com> wrote:  
 
 Hi again,
I solved the unexpected error I just posted by following this link:
https://mail.python.org/pipermail/python-list/2008-July/509467.html
There is said to:

"Just in case, could you delete the contents of your gen_py
directory (probably in %TEMP%\gen_py)"That did it.
Now I was able to test your answer. But I got this error:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-3745e339d45c> in <module>()
----> 1 ImageMap1 = ImageLayer1.CastTo("IContourMap")

C:\Program Files\Anaconda3\lib\site-packages\win32com\client\__init__.py in __getattr__(self, attr)
    463                 args=self._prop_map_get_.get(attr)
    464                 if args is None:
--> 465                         raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
    466                 return self._ApplyTypes_(*args)
    467 

AttributeError: '<win32com.gen_py.Surfer 13 Type Library.IShape instance at 0x2630076970544>' object has no attribute 'CastTo' 

I tried adding an import for it to work:

import win32com.client
from win32com.client import CastTo  # tried adding this line

but it didn't change the error.

E m r e
 

    On Tuesday, May 22, 2018, 11:09:45 PM GMT+3, Emre CETIN via python-win32 <python-win32 at python.org> wrote:  
 
 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
  _______________________________________________
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/74637229/attachment-0001.html>


More information about the python-win32 mailing list