Openpyxl change the dimension of a chart

Lornioiz picture Lornioiz · Apr 13, 2016 · Viewed 7.1k times · Source

In openpyxl 2.2 I was able to set the dimension of a chart by using the drawing methods of the chart object:

chart = openpyxl.charts.BarChart()
chart.drawing.top = 100
chart.drawing.left = 100
chart.drawing.width = 600
chart.drawing.height = 600

In the new versions (>2.3.3), this methods seems to not exist anymore. I read the online manual and i find as follows:

"The chart can be positioned within its container. x and y adjust position, w and h adjust the size . The units are proportions of the container. A chart cannot be positioned outside of its container and the width and height are the dominant constraints: if x + w > 1, then x = 1 - w."

So I thought that I had to change the container first. I searched in the manual the class class openpyxl.chart.chartspace.ChartContainer but I was not able to find a method to set its width and height.

How can I display a chart with fixed custom dimensions?

Answer

Charlie Clark picture Charlie Clark · Apr 13, 2016

The code for charts was completely rewritten in openpyxl 2.3. You can now set the approximate width and height in cm directly on your chart:

chart = openpyxl.chart.BarChart()
chart.height = 10 # default is 7.5
chart.width = 20 # default is 15

All chart measurements internally rely upon conversion to the operating system and can, thus, vary significantly between systems.