Python-pptx OxmlElement - Chart background color only changes plotArea element, leaves outer part transparent

72 Views Asked by At

I apologize up front, I am not a real programmer, just someone hacking my way through things.

I have created a powerpoint with a line chart in it, with multiple series. And I figured out how to change the plot area color, but the area around it has remained transparent and I cannot figure out how to access the outer ring of the chart to change the color.

I've tried many different things, including trying to figure out how to access the placeholder and make that white instead. I've spent a lot of time poring over all the python-pptx documentation and issues, searching stack overflow, trying different suggested methods, and none of it seems to be touching the outer rim.

One more question if I can, how do I make the series line weight thinner? Also spent some time on that with so far no solution.

Here is the current state of the code:

graph_slide = prs.slide_layouts[5]
slide = prs.slides.add_slide(graph_slide)
background_object = slide.background
fill = background_object.fill
fill.solid()
fill.fore_color.rgb = RGBColor(0, 0, 0) 
# this is why I need to give the chart a background color

chart_data = ChartData()
chart_data.categories = df['x_axis_values'].tolist()
measure1 = df['measure1'].tolist()
measure2 = df['measure2'].tolist()
measure3 = df['measure3'].tolist()
chart_data.add_series('measure1', measure1)
chart_data.add_series('measure2', measure2)
chart_data.add_series('measure3', measure3)

x, y, cx, cy = Inches(.5), Inches(1), Inches(15), Inches(3.75)
chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data).chart

chart.chart_title.has_text_frame = True
chart.chart_title.text_frame.text = 'Chart Title'

chart.value_axis.major_gridlines.format.line.color.rgb = RGBColor(132, 136, 132)
series1 = chart.series[0]
line1 = series1.format.line
line1.color.rgb = RGBColor(15, 82, 186)
series2 = chart.series[1]
line2 = series2.format.line
line2.color.rgb = RGBColor(210, 43, 43)
series3 = chart.series[2]
line3 = series3.format.line
line3.color.rgb = RGBColor(70, 200, 90)

plot_area = chart._element.chart.plotArea 
# I can access plotArea and want it to remain as grey
shape_properties = OxmlElement("c:spPr")
plot_area.append(shape_properties)
fill_properties = OxmlElement("a:solidFill")
shape_properties.append(fill_properties)
rgb_color = OxmlElement("a:srgbClr")
color_value = dict(val='%02x%02x%02x' % (229, 228, 226))
rgb_color.attrib.update(color_value)
fill_properties.append(rgb_color)

What I get:

enter image description here

What I want:

enter image description here

EDIT 19-JAN-2024: I figured out how to look at the xml code for the powerpoint slide, and manually modified one of my charts to either have a white chart background or a green one, to make it easier to see where it changes. Here is what I found:

Graphs done manually: enter image description here

xml code: enter image description here

So my more specific question is, how do I use pptx with xml to change the chartSpace element, or add a row like is there for the rounded corner element, to fill in the chart background?

Something like:

#  THIS WORKS ON THE OBJECT CORNERS
xml = '<c:roundedCorners val="0"/>'
parser = etree.XMLParser(recover=True)
element = etree.fromstring(xml, parser)
chart._chartSpace.append(element)

# HOW TO DO THIS?
xml = '<c:background val="white"/>'
parser = etree.XMLParser(recover=True)
element = etree.fromstring(xml, parser)
chart._chartSpace.append(element)
0

There are 0 best solutions below