Can't set cell background colour

1,615 views
Skip to first unread message

dhe...@gmail.com

unread,
Jan 27, 2016, 8:59:36 AM1/27/16
to openpyxl-users
I've started using openpyxl and I'm very impressed.
But I have a problem setting background in a cell.
I reduced the problem to this short program.
    import openpyxl
    wb = openpyxl.Workbook()
    ws=wb.active
    ws['A1'] = 'Red background'
    fill_style = ws['A1'].fill.copy()
    fill_style.bgColor = 'ff0000'
    wb.save('good.xlsx') # A1 has white background as expected.
    ws['A1'].fill = fill_style
    wb.save('bad.xlsx') # A1 should have red background but doesn't.

Using openpyxl 2.3.3, Winpython 2.7.6.4 (32 bit), Windows 7 (64 bit).

Am I doing something wrong or is there a bug?

Thanks
Dominic

Charlie Clark

unread,
Jan 27, 2016, 10:49:03 AM1/27/16
to openpyxl-users, dhe...@gmail.com
Am .01.2016, 14:59 Uhr, schrieb <dhe...@gmail.com>:

> I've started using openpyxl and I'm very impressed.

We're glad you like it.

> But I have a problem setting background in a cell.
> I reduced the problem to this short program.
> import openpyxl
> wb = openpyxl.Workbook()
> ws=wb.active
> ws['A1'] = 'Red background'
> fill_style = ws['A1'].fill.copy()
> fill_style.bgColor = 'ff0000'
> wb.save('good.xlsx') # A1 has white background as expected.
> ws['A1'].fill = fill_style
> wb.save('bad.xlsx') # A1 should have red background but doesn't.
>
> Using openpyxl 2.3.3, Winpython 2.7.6.4 (32 bit), Windows 7 (64 bit).
>
> Am I doing something wrong or is there a bug?

The problem is that fills either gradients or patterns and pattern fills
must have a type to be visible. The next thing you'll notice is that you
actually need to be setting the colour for the foreground (of the pattern).

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

Thomas Nygårds

unread,
Jan 29, 2016, 3:21:54 AM1/29/16
to openpyxl-users, dhe...@gmail.com
If you use eg. Patternfill this will ge done for you.

 ws['A1'].fill=PatternFill(start_color= 'ff0000', end_color= 'ff0000', fill_type="solid")


 

Natarajan Muthu

unread,
Mar 9, 2016, 10:48:03 PM3/9/16
to openpyxl-users, dhe...@gmail.com
import openpyxl
import sys
import os
import subprocess
import time
import datetime
import cmd

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'],ws['B1'],ws['C1'],ws['D1'],ws['E1'],ws['F1'] = ('S.NO'),('DESCRIPTION'),('KVB DC STM -1'),('KVB DC STM -2'),('KVB DR STM -1'),('KVB DC STM -2')
ws['A2'],ws['A3'],ws['A4'],ws['A5'] = 1 , 2 , 3 , 4
ws['A1'].fill=PatternFill(start_color= 'ff0000', end_color= 'ff0000', fill_type="solid")
print('Work Book Created.....')
wb.save("KVB CORE LINKS STABILITY REPORT .xlsx")

I am getting following error.Please correct .
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Open excel.py", line 14, in <module>
    ws['A1'].fill=PatternFill(start_color= 'ff0000', end_color= 'ff0000', fill_type="solid")
NameError: name 'PatternFill' is not defined

Thomas Nygårds

unread,
Mar 10, 2016, 7:11:11 AM3/10/16
to openpyxl-users, dhe...@gmail.com
You need to import it.


from openpyxl.styles import PatternFill

or use
openpyxl.styles.PatternFill(....)

in your code.
Reply all
Reply to author
Forward
0 new messages