On Jun 20, 2:23 am, Chanman <
bryancc...@gmail.com> wrote:
> How does one change the cell background color using the xlwt module?
> I've looked at several tutorials but none show how to change the
> background color. I've tried the following:
>
> badBG = xlwt.Pattern()
> badBG.SOLID_PATTERN = 0x34
> badBG.NO_PATTERN = 0x34
Python convention is that UPPER_CASE is used for pseudo-constants --
assigning different values to them is at best pointless and at worst
could make things blow up.
You need
badBG.pattern = badBG.SOLID_PATTERN
to set what type of pattern you want. The default is NO_PATTERN.
> badBG.pattern_fore_colour = 0x34
> badBG.pattern_back_colour = 0x34
Setting pattern_back_colour is neither necessary nor useful for a
SOLID_PATTERN -- you only see the pattern_fore_colour. On the other
hand, for any other pattern type, you need to set pattern_fore_colour
and pattern_back_colour to two different colours; if they are the
same, you would get the same effect as SOLID_PATTERN.
>
> badFontStyle = xlwt.XFStyle()
"Font" is irrelevant and/or potentially misleading here -- but I
should address my grumble to the author of the blog from which you
copied this :-)
> badFontStyle.Pattern = badBG
>
> sheet1.write(1,1,'hello world', badFontStyle)
>
> However, the background cell color still remains white. Any help?
HTH,
John