我正在使用带有以下代码的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")
这在折线图上(对组合图而言)对我有用: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
希望这对某人有帮助
数据标签是按图表,系列甚至系列中的每个项目设置的。目前,您必须查看源代码并确定其工作方式,但是chart.dataLabels
或chart.series[0].label
是开始的地方。