Reading font color and cell fill color

2,335 views
Skip to first unread message

Gericke Potgieter

unread,
Feb 2, 2016, 4:10:30 AM2/2/16
to openpyxl-users
I need to perform a simple test that would compare the font color of a cell to its fill color.  For example, white font on white background (or no background) would generate True.

My initial attempt was as follows:

def has_font_back_color(t_cell):
"""
Checks if the cell foreground and background are the same color in an attempt to hide cells
:param t_cell: a cell with formatting information
:return: fb_check: a boolean indicating whether font and background is the same color
"""

fb_check = False

if not t_cell.value is None:
f_color = t_cell.font.color.index
  b_color = t_cell.fill.start_color.index

if f_color == b_color:
fb_check = True

return fb_check

In the function t_cell is a cell variable passed to the function from a main program that sequentially reads and tests cells from a given range. When printing the index values the console indicates 00000000 for both the font and background color. Using .rgb generates the same values. When using .color the console indicates the following:

Color(rgb='00000000', indexed=Values must be of type <class 'int'>, auto=Values must be of type <class 'bool'>, theme=Values must be of type <class 'int'>, tint=0.0, type='rgb').


I confirmed with Charlie Clark that this is not a bug as the cells do not initialize with their font and background colors in the way I imagined.  I need more input on how to achieve this goal without having to analyze the formatting in the XML itself.  Any ideas?

Charlie Clark

unread,
Feb 2, 2016, 5:33:13 AM2/2/16
to openpyx...@googlegroups.com
Am .02.2016, 10:10 Uhr, schrieb Gericke Potgieter
<gericke....@gmail.com>:

> I need to perform a simple test that would compare the font color of a
> cell
> to its fill color. For example, white font on white background (or no
> background) would generate True.

You will need to familiarise yourself with the details of the
specification regarding styles, particularly colours and fills. First of
all, as well as checking whether a cell has a value, you should also check
whether it has a style. Then you should check type of fill and patternType
if it is a PatternFill.

You can check for colour equivalence directly:

if c.fill.fgColor == c.font.color:


Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Gericke Potgieter

unread,
Feb 5, 2016, 3:24:38 AM2/5/16
to openpyxl-users
Thanks, I already check for values, formulas and other attributes, but I will add a check for styles.  Once solved I will share the code here.

dmg...@gmail.com

unread,
Feb 22, 2016, 7:22:03 AM2/22/16
to openpyxl-users
Did you manage to do this? I would appreciate if you could share it because I'm running through a very similar issue. Thanks.

Gericke Potgieter

unread,
Feb 22, 2016, 8:05:44 AM2/22/16
to openpyxl-users, dmg...@gmail.com
Hi,
I have unfortunately not yet been able to solve this problem (mostly because I was busy on another project).  I will post a solution when I have one.

Luis Fernando

unread,
Apr 11, 2025, 9:29:11 AMApr 11
to openpyxl-users

Hi, you solved the problem?

Buna sorn

unread,
Apr 14, 2025, 10:22:48 AMApr 14
to openpyx...@googlegroups.com
what you cut my money stole from out from my card


--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/openpyxl-users/2eb250ed-fa3a-4a33-b2e3-536b0480f703n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages