如何使用 openpyxl 在 excel 图表中仅显示一个数据标签?
How to show only one data label in excel chart using openpyxl?
我使用以下代码创建了一个简单的 excel sheet 和一个使用 openpyxl
的图表(代码来自 documentation - 编辑以解释需要)
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.label import DataLabelList
wb = Workbook()
ws = wb.active
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 41, 30, 25],
[date(2015,9, 2), 41, 25, 30],
[date(2015,9, 3), 41, 30, 45],
[date(2015,9, 4), 41, 25, 40],
[date(2015,9, 5), 41, 35, 30],
[date(2015,9, 6), 41, 40, 35],
]
for row in rows:
ws.append(row)
c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)
s2 = c1.series[2]
s2.smooth = True # Make the line smooth
c1.dataLabels = DataLabelList()
###########################################################
#Display data label and series name
#I need this to be displayed only for the first data point
#I can do this in excel by displaying the label only for the
#data point required
c1.dataLabels.showVal = True
c1.dataLabels.showSerName = True
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
我得到的图表
我想要的图表 - 我怎样才能得到这样的图表?
仅为一个数据点显示标签(系列名称和值)...
我成功了,这里是带有解释的代码:
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.label import DataLabel
wb = Workbook()
ws = wb.active
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 41, 30, 25],
[date(2015,9, 2), 41, 25, 30],
[date(2015,9, 3), 41, 30, 45],
[date(2015,9, 4), 41, 25, 40],
[date(2015,9, 5), 41, 35, 30],
[date(2015,9, 6), 41, 40, 35],
]
for row in rows:
ws.append(row)
c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)
#Get the first series
s1 = c1.series[0]
#Initialize data lables
s1.dLbls = DataLabelList()
#Initialize data label
dl = DataLabel()
#Set properties
dl.showVal = True
dl.showSerName = True
#position t for top
dl.position = "t"
#Append data label to data lebels
s1.dLbls.dLbl.append(dl)
#This produces expected result
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
结果
我仍然无法设置标签文本属性!
我使用以下代码创建了一个简单的 excel sheet 和一个使用 openpyxl
的图表(代码来自 documentation - 编辑以解释需要)
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.label import DataLabelList
wb = Workbook()
ws = wb.active
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 41, 30, 25],
[date(2015,9, 2), 41, 25, 30],
[date(2015,9, 3), 41, 30, 45],
[date(2015,9, 4), 41, 25, 40],
[date(2015,9, 5), 41, 35, 30],
[date(2015,9, 6), 41, 40, 35],
]
for row in rows:
ws.append(row)
c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)
s2 = c1.series[2]
s2.smooth = True # Make the line smooth
c1.dataLabels = DataLabelList()
###########################################################
#Display data label and series name
#I need this to be displayed only for the first data point
#I can do this in excel by displaying the label only for the
#data point required
c1.dataLabels.showVal = True
c1.dataLabels.showSerName = True
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
我得到的图表
我想要的图表 - 我怎样才能得到这样的图表?
仅为一个数据点显示标签(系列名称和值)...
我成功了,这里是带有解释的代码:
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.label import DataLabel
wb = Workbook()
ws = wb.active
rows = [
['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
[date(2015,9, 1), 41, 30, 25],
[date(2015,9, 2), 41, 25, 30],
[date(2015,9, 3), 41, 30, 45],
[date(2015,9, 4), 41, 25, 40],
[date(2015,9, 5), 41, 35, 30],
[date(2015,9, 6), 41, 40, 35],
]
for row in rows:
ws.append(row)
c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)
#Get the first series
s1 = c1.series[0]
#Initialize data lables
s1.dLbls = DataLabelList()
#Initialize data label
dl = DataLabel()
#Set properties
dl.showVal = True
dl.showSerName = True
#position t for top
dl.position = "t"
#Append data label to data lebels
s1.dLbls.dLbl.append(dl)
#This produces expected result
ws.add_chart(c1, "A10")
wb.save("line.xlsx")
结果
我仍然无法设置标签文本属性!