Cell background color

3,679 views
Skip to first unread message

gabriele...@gmail.com

unread,
Jan 6, 2016, 3:27:33 AM1/6/16
to openpyxl-users
Hello,
someone of you can help me to understand how to to set a background color for a cell?

version of openpyxl is 2.3.2

thank you in advance
Workino

Charlie Clark

unread,
Jan 6, 2016, 4:37:23 AM1/6/16
to openpyx...@googlegroups.com
Am .01.2016, 09:27 Uhr, schrieb <gabriele...@gmail.com>:

> someone of you can help me to understand how to to set a background color
> for a cell?

You want to use a solid fill for the cell to do this.

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

gabriele...@gmail.com

unread,
Jan 6, 2016, 6:31:15 AM1/6/16
to openpyxl-users
yes but how?
Message has been deleted

Thomas Nygårds

unread,
Jan 7, 2016, 7:14:59 AM1/7/16
to openpyxl-users, gabriele...@gmail.com
like so, for example:

from openpyxl import Workbook
from openpyxl.styles import PatternFill
wb
= Workbook()

ws
=wb.create_sheet(title="test_sheet")

rgb
=[255,0,0]
color_string
="".join([str(hex(i))[2:].upper().rjust(2, "0") for i in rgb])
ws
["A1"].fill=PatternFill(fill_type="solid", start_color='FF' + color_string, end_color='FF' + color_string)

wb
.save("test_background.xlsx")

Charlie Clark

unread,
Jan 7, 2016, 7:51:06 AM1/7/16
to openpyx...@googlegroups.com
Am .01.2016, 13:14 Uhr, schrieb Thomas Nygårds <thomas...@gmail.com>:

> rgb=[255,0,0]
> color_string="".join([str(hex(i))[2:].upper().rjust(2, "0") for i in
> rgb])
> ws["A1"].fill=PatternFill(fill_type="solid", start_color='FF' +
> color_string
> , end_color='FF' + color_string)

Ugh!

rgb = [255,0,0]
colour = "{0:02X}{1:02X}{2:02X}".format(*rgb)
fill = PatternFill(patternType="solid", fgColor=colour, bgColor=colour)

There is no need to prefix with "FF": this is the default alpha value and
will be added automatically if missing.

Thomas Nygårds

unread,
Jan 7, 2016, 8:12:02 AM1/7/16
to openpyxl-users
Thanks! Nice format string, and probably faster! Did not know about it.


"{0:02X}{1:02X}{2:02X}".format(*rgb)




Charlie Clark

unread,
Jan 7, 2016, 8:19:11 AM1/7/16
to openpyx...@googlegroups.com
Am .01.2016, 14:12 Uhr, schrieb Thomas Nygårds <thomas...@gmail.com>:

> Thanks! Nice format string, and probably faster! Did not know about it.

Well, I had to look it up myself but your code looked so bad I knew
something else would be possible! ;-) It will be significantly faster,
though I can't imagine you'd notice in any openpyxl code.

hex() always returns a string. Redundant casting to strings should be
avoided, not least for the chance that unicode issues might crop up in
Python 2 code.

FWIW: well worth reading
https://docs.python.org/3/library/string.html#formatstrings

Some of the tricks inherited from printf style are non-obvious {:02X} #
":" announces additional instructions; "X" says uppercase hex; "02" says 2
chars wide, pad with 0 if necessary. I wonder if there's a site where you
can test this stuff out like you can with regexes?

mr2...@gmail.com

unread,
Mar 31, 2016, 8:38:49 PM3/31/16
to openpyxl-users, gabriele...@gmail.com
Thank you!
Reply all
Reply to author
Forward
0 new messages