Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show data labels in charts created via Openpyxl

I am creating charts using openpyxl with the following code. By default it doesn't show data labels - so I have to right-click the chart and choose 'Add Data Labels" manually. How do I do that with Openpyxl command? Thanks in advance !

data = Reference(ws, min_col=2, min_row=1, max_col=6, max_row=10)
titles = Reference(ws, min_col=1, min_row=2, max_row=10)
chart = BarChart3D()
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)
ws.add_chart(chart, "C10")
like image 938
Gokul Krishnan Avatar asked Mar 14 '23 23:03

Gokul Krishnan


1 Answers

This works for me on a line chart (As a combination chart): openpyxls version: 2.3.2:

from openpyxl.chart.label import DataLabelList
chart2 = LineChart()
.... code to build chart like add_data()
and: # Style the lines
s1 = chart2.series[0]
s1.marker.symbol = "diamond"
... your data labels added here:
chart2.dataLabels = DataLabelList() 
chart2.dataLabels.showVal = True

Hope this helps someone

like image 57
Mattman85208 Avatar answered Apr 30 '23 05:04

Mattman85208