How to change the background colour of a chart

538 views
Skip to first unread message

miles...@gmail.com

unread,
Aug 21, 2016, 7:12:30 PM8/21/16
to openpyxl-users
Hi, 

I'm looking into formatting my charts, but can't see anything that addresses changing the background colour of them. 

I found the chart.picture section of the documentation, but also can't figure out how to use that either. Can this be applied to a LineChart()? If so, how do I specify the location of the image? 

All I really want to do is to make the chart background black, but a picture/gradient would be better :) 

Cheers,
Miles 

Charlie Clark

unread,
Aug 22, 2016, 9:50:49 AM8/22/16
to openpyx...@googlegroups.com
The simple answer is that the documentation is incomplete. The more
detailed answer is that you need to work with the DrawingML specification
which is common to all MS Office products and has its own namespace and,
er, quirks.

Fortunately, as long as you're prepared to do some reverse engineering of
existing files, this is pretty easy to do: add a solid fill both the
"chart space" and the "plot area". The code for both is identical and for
solid fills it has even been optimised.

from openpyxl.chart.shapies import GraphicalProperties

props = GraphicalProperties(solidFill="000000")
chart.graphical_properties = props
chart.plot_area.graphical_properites = props

This is untried and untested so it may not work quite as expected.
Gradient Fills should be possible but you'll basically have to work those
out yourself.

Additions to the documentation are always welcome.

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

miles...@gmail.com

unread,
Sep 9, 2016, 6:47:34 PM9/9/16
to openpyxl-users
Thanks Charlie! 
Reply all
Reply to author
Forward
0 new messages