[python-win32] Cannot assign values to the Excel Colors property
Joe Goldthwaite
joe at goldthwaites.com
Tue Aug 24 22:17:10 CEST 2004
Hi Graham,
Sorry, I'm still a beginner at Python and don't really understand what the
MakePy utility is doing. I started off playing with it last month and was
able to generate a big Excel.py file but I couldn't import it or anything.
Since your sample shows that the Dispatch function is using it somehow, I
went back to the Web to try and figure out what you are talking about.
I found another reference that describes running MakePy from the PythonWin
menu. I tried that and it created a big file in the win32com\gen_py folder
under a name that looks like it's probably Excel's GUID. I gather that when
you try to create an Excel application with Dispatch, it somehow finds that
file and imports it.
After running MakePy, the SetColors function did work! That's pretty slick.
It must be doing a lot of stuff behind the scenes. Do you know if MakePy
does early binding? Do you know what the performance difference is (good or
bad)? Not that it matters. I'd rather have clean code than high performing
code. I'll replace my Macro kludge with SetColors. I'd like to know what's
going on though.
Thanks for taking the time to respond. I really appreciate it.
Joe Goldthwaite
-----Original Message-----
From: Graham Bloice [mailto:graham.bloice at trihedral.com]
Sent: Tuesday, August 24, 2004 11:45 AM
To: joe at goldthwaites.com
Cc: python-win32 at python.org
Subject: RE: [python-win32] Cannot assign values to the Excel Colors
property
> Graham,
>
> I tried the SetColors(1, xxx) but I get an error message that there is not
> SetColors attribute. I looked for it in my Excel VBA language
> reference and
> couldn't find it there either.
As I (thought I had) explained, the Python COM support doesn't directly
support the VB notion of index properties, i.e. Workbook.Colors(1) =
&H254A70. Colors is a property of the Workbook object that has to be
indexed. The Python COM support turns the property access into a method
access by prepending either Get, for a property read, or Set, for a property
write to the name of the property and passing the index value as a parameter
of the method.
>>> xl = Dispatch("Excel.Application")
>>> xl
<win32com.gen_py.Microsoft Excel 9.0 Object Library._Application instance at
0x23987600>
>>> xl.Visible = -1
>>> wb = xl.Workbooks.Add()
>>> wb
<win32com.gen_py.None.Workbook>
>>> wb.GetColors(2)
16777215.0
>>> wb.SetColors(2, 0)
And sure enough, if I open the Format | Cells | Patterns dialog, the second
color in the palette is black. Note that you can't set the color when the
dialog is open.
Are you sure you've run the MakePy utility on Excel ??
Graham
More information about the Python-win32
mailing list