Search code examples
pythonexcelxlsxxlsxwriter

Bar colouring in XlsxWriter conditioned on positive/negative values


I am working with XlsxWriter in Python, and am trying to generate a column chart in which positive values would be colored green and negative values would be colored red.

Is this doable? In Excel, I would accomplish this by turning on the "invert if negative" fill switch, and there, I would specify my colors. I can specify the default color in Pyhton and either turn on the "invert_if_negative" switch, however - I am not sure whether I can, and if yes, how - specify the second, negative color in Xlsx?

  chart.add_series({'name': 'Name',
                    'categories': [self.sheet_name, header[0], header[1], header[2], header[3]],
                    'values': [self.sheet_name, section[0], sekction[1], sekction[2], sekction[3]],
                    'fill': {'color': "#141E50")},
                    'invert_if_negative':True})

Many thanks!


Solution

  • The ability to define the colour of the inverted/negative values was only added in recent (2015+?) versions of Excel and as such isn't supported by XlsxWriter.