[python-win32] Formatting parts of an excel cell

Mark Tolonen metolone+gmane at gmail.com
Mon Apr 27 17:06:17 CEST 2009


"Baehr, Manuel" <Manuel.Baehr at comsoft.aero> wrote in message 
news:737A64DEB003704788E5CE515ED146054B6D5E at CSMAIL.comsoft.de...
> Hi Mark,
>
>> I got an error with that as well, but I figured out the correct way:
>>
>> import win32com.client
>> xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
>> xl.Visible = True
>> xl.Workbooks.Add()
>> c = xl.ActiveCell
>> c.FormulaR1C1 = 'Hello World'
>> c.GetCharacters(1,5).Font.ColorIndex = 3
>> c.GetCharacters(7,5).Font.ColorIndex = 4
>
> thanks for your answer, works like a charm!
>
> I tried the macro-way as well but stopped because that didn't work.
> How/where did you find out the correct way to use this feature
> (GetCharacters instead of Characters)? This answer may prevent me from
> asking further questions of this kind ;-)
>
> Cheers,
> Manuel

I couldn't find where it was documented, although I recalling reading about 
it years ago and had forgotten about it.  In this case I was using PythonWin 
as the editor, and with the EnsureDispatch call popup tooltips are enabled. 
Typing "c." listed the "GetCharacters" and "SetCharacters" methods, but I 
didn't notice it immediately.  Too bad the convention wasn't "CharactersGet" 
and "CharactersSet" or I would have found it quicker in the sorted tooltip 
list :^)

-Mark




More information about the python-win32 mailing list