Similar questions have been asked, but I still can't get it to work despite multiple tries. Python PPTX Bar Chart negative values , Color Specific Bar Chart Differently in Python PPTX
Here is a simple example:
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm
from pptx.dml.color import RGBColor
_red = RGBColor(255, 0, 0)
_green = RGBColor(0, 128, 0)
# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
x = ['one','two','three', 'four']
y = [-257.0, 1920.0, 2809.0, 500.0]
specs = {
'height': Cm(7.82),
'width': Cm(14.8),
'left': Cm(2.53),
'top': Cm(5.72)}
data = ChartData()
data.categories = x
label_values = tuple(y)
data.add_series('Series 1', label_values)
frame = slide.shapes.add_chart(XL_CHART_TYPE.BAR_CLUSTERED,
specs['left'],specs['top'], specs['width'],specs['height'], data)
chart = frame.chart
series = chart.series[0]
for i in range(0,len(y)):
point = series.points[i]
fill = point.format.fill
fill.solid()
fill.fore_color.rgb = _red if y[i]<0 else _green
series.invert_if_negative = False
prs.save('chart-01.pptx')
I have tried many different configurations. Python-pptx is working, because by setting the boolean to true or false in my code with series.invert_if_negative the checkbox acts accordingly when I open powerpoint presentation as shown in the second image. It only displays correctly in powerpoint if I manually toggle it a couple times after the presentation has already been generated. I tried changing the location of the statement, setting it to true or false and even having it alternate multiple times before settling on false.
Interestingly this only happens with negative bar chart i.e. with positive values I can change the color as I please. Even if all the values are negative. I can't set them all to a certain color. The only time they have color and don't display as white boxes is when I don't try to change their color at all and they default to the presentation theme colors.
I suspect this is a powerpoint issue rather than pptx-python problem, but any help in resolving this will be much appreciated!
Powerpoint version: 15.36 on Mac Python-pptx version: 0.6.7
Thanks for the amazing library Scanny and your responses to all the other questions. It has been loads of help.
=======UPDATE=======
There is a workaround to this problem which is to use the patterned fill and set both back_color and fore_color to the same color to achieve the same effect.