如何在通过Openpyxl创建的图表中显示数据标签

Gok*_*nan 3 charts label openpyxl

我正在使用带有以下代码的openpyxl创建图表。默认情况下,它不显示数据标签-因此,我必须右键单击该图表并手动选择“添加数据标签”,该如何使用Openpyxl命令来完成呢?

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")
Run Code Online (Sandbox Code Playgroud)

Mat*_*Cox 5

这对我在折线图上有效(作为组合图):openpyxls版本: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
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助某人