24

I am using python 2.7 and xlwt module for excel export

I would like to set backgroung colour of a cell i know i can use

style1 = xlwt.easyxf('pattern: pattern solid, fore_colour red;')

but I would like to set custom color smth. like #8a8eef or is there a palette of possible colors, because light blue is not working :)

thank you

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Yebach
  • 1,661
  • 8
  • 31
  • 58

5 Answers5

35

Nowadays, there is a way (originally proposed here) to define and use custom colour using add_palette_colour() and set_colour_RGB().

Here's an example:

import xlwt


book = xlwt.Workbook()

# add new colour to palette and set RGB colour value
xlwt.add_palette_colour("custom_colour", 0x21)
book.set_colour_RGB(0x21, 251, 228, 228)

# now you can use the colour in styles
sheet1 = book.add_sheet('Sheet 1')
style = xlwt.easyxf('pattern: pattern solid, fore_colour custom_colour')
sheet1.write(0, 0, 'Some text', style)

book.save('test.xls')

Also see the actual pull request that made this happen.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    In your example, you used `0x21`. Is there any reason you picked that? If I want to add more, should I use `0x22`, `0x23`, and so on? – Nick Oct 13 '16 at 20:42
  • @Nick [source code](https://github.com/python-excel/xlwt/blob/917a8ad8db35d6e8abb306a2fda2ace648a6ab89/xlwt/Style.py#L374) suggests that `0x21` can be any number in range `8 <= colour_index <= 63` – moriesta Nov 03 '16 at 20:13
  • Note that you can omit `add_palette_colour` by overwriting an existing palette color. Example: `book.set_colour_RGB(0x16, 245, 245, 245); style = xlwt.easyxf('pattern: pattern solid, fore_colour gray25')` – Blaise Jan 05 '17 at 10:51
  • NB: although `add_palette_colour()` accepts upper case letters in color names, trying to use them in `easyxf()` fails. Make sure you apply `.lower()` if needs be. – Arnaud P Aug 31 '18 at 12:36
13

If you are not using easyxf() and instead are building XFStyle object step by step, here is another way of using user friendly color names:

import xlwt

style = xlwt.XFStyle()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = xlwt.Style.colour_map['dark_purple']
style.pattern = pattern
Frank
  • 64,140
  • 93
  • 237
  • 324
darklow
  • 2,249
  • 24
  • 22
6

For predefined colors see xlwt.Style._colour_map_text in Style.py.

To use custom colors you will have to probably redefine palette, because colors are not used directly in cells but as a index to color in a palette. I don't know how to extend palette. Sorry.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
2

Alternative solution:

If you can get away with the colors defined in xlwt, go to a color information site like http://www.colorhexa.com/90ee90 and match from one of the following Python Excel colors: http://bit.ly/1NMH67F

Sandeep
  • 28,307
  • 3
  • 32
  • 24
2

I recommend to use XlsxWriter, has awesome features too. http://xlsxwriter.readthedocs.io/

Angie Alejo
  • 743
  • 9
  • 15
  • 1
    XlsxWriter has more intuitive API b/c it uses key value pairs to set the formats – Wes Jul 21 '17 at 19:36
  • 1
    xlwt is for old xls format, while xlsxwriter is for the new format, so they're not interchangeable. – Gregory Mar 15 '21 at 15:12