[python-win32] XL Worksheet and formulas

Tim Golden tim.golden at viacom-outdoor.co.uk
Tue Dec 23 09:30:30 EST 2003


>From: Kojo Idrissa [mailto:kojo at hal-pc.org]
>John,
>If you're just trying to do it in Excel (no Python), you can just use 
>Conditional Formatting.
>
>If you're trying to do it in Python, I think you'd have to use Python 
>to alter the value of the FontColorIndex attribute, based on the value 
>in the cell.
>
>If you want code (in VB or Python), I may be able to come up with 
>something.  The "Excel Only" version needs no code.
>
>On Thu, 18 Dec 2003 02:30:13 +0100
>  "John A.Simonsen" <jasimons at online.no> wrote:
>]Hi there.
>]
>]I wondered if you or anybody knows how to make the numbers in a cell 
>]to change color.
>]Problem!!! I want the numbers in the cell(s) to change color if the 
>]value is zero, or have a negative value.
>]
>]Is there anybody that can help me out here???  I have spent a lot of 
>]time trying to find a way, but I still haven’t had any success.
>]
>]Best regards.                Phone.        +47 51 62 70 98        
>]                          Cell Phone.  +47 913 20 009 
>]                        
>]John A. Simonsen.         Fax.             +47 51 97 77 56           
>]jasimons at online.no           Fax.             +47 51 68 99 05

Don't know if this one's been sorted out yet, but I use the code below
 in my general purpose sql2xl library, which I think is the kind of 
 thing you want. I wrote it a long time ago, but almost certainly
 it was derived from recording a macro in Excel and then translating the
 resulting VBA:

<code>
r = ws.Range (ws.Cells (2, 1 + col), ws.Cells (1 + n_rows, 1 + col))
r.FormatConditions.Add (win32com.client.constants.xlCellValue,
win32com.client.constants.xlGreater, 0).Interior.Color = RGB (0, 255, 0) #
green => value > 0
r.FormatConditions.Add (win32com.client.constants.xlCellValue,
win32com.client.constants.xlLess, 0).Interior.Color = RGB (255, 0, 0) # red
=> value < 0
r.FormatConditions.Add (win32com.client.constants.xlCellValue,
win32com.client.constants.xlEqual, 0).Interior.Color = RGB (255, 255, 255) #
white => value == 0
</code>

This is only a fragment, and the n_rows and col and so on are not going
 to mean much to you, but I believe the principle should be clear. The
 RGB function is imported from win32api.

TJG


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-win32 mailing list