在 openpyxl 中格式化图表数据标签

Formatting chart data labels in openpyxl

我正在使用 Python 3.6.3 和 openpyxl (2.4.9) 编写一些 excel 表格。在图表数据上获取数据标签并不明显,但当我尝试格式化所述数据标签时,事情开始变得糟糕。我想要做的是改变他们的位置,也改变他们的轮换。有人有什么想法吗?我对 python 有点陌生,所以任何建议都很好。 这是我尝试过的:

from openpyxl.chart import LineChart, Reference, Series
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import RichTextProperties

#this is the only way I found to set text properties which didnt give an error
vertical = RichTextProperties(rot = 270)
labelvertical = RichText(bodyPr = vertical)


chart1 = LineChart()

# setup and append the first series
values = Reference(dprsheet, min_col=5, min_row=4, max_row=34)
series = Series(values, title="Series 1")
chart1.append(series)

# setup and append the second series
values = Reference(dprsheet, min_col=8, min_row=4, max_row=34)
series = Series(values, title="Series 2")
chart1.append(series)

dates = Reference(dprsheet, min_col=2, min_row=4, max_row=34)

chart1.set_categories(dates)

s1 = chart1.series[0]
s1.graphicalProperties.line.solidFill = 'FF0000'
s1.graphicalProperties.line.width = 25000
s1.dLbls = DataLabelList() 
s1.dLbls.showVal = True

## s1.dLbls.dLblPos = 't'
## if ^this^ line isn't commented then ALL images in the sheet are removed by excel upon opening
## s1.dLbls.txPr = labelvertical
## if ^this^ line isn't commented then ALL images in the sheet are removed by excel upon opening
s2 = chart1.series[1]
s2.graphicalProperties.line.solidFill = '000000'
s2.graphicalProperties.line.width = 25000

essheet.add_chart(chart1, 'B35')

抱歉,恐怕您触及了 OOXML 规范的限制,这在这方面并不总是很清楚。本质上,在这个级别上,openpyxl 将 XML 架构公开给 Python,因此您最好手头有一份规范的副本以及由 Excel 创建的可比较文件,以查看它的作用.

举个未记录的例子:如果你想要图表线有不同的颜色,你必须在 DrawingML 的相关部分 lumMod 之前有 lumOff

我们永远无法在 openpyxl 中记录所有这些内容。

我最终到达了那里,通过尝试弄清楚如何更改轴标签...这应该可以扩展到任何更改(使用字体等的段落属性,或对齐等的正文属性) .

from openpyxl.chart import LineChart, Reference, Series
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.text import RichText
#additional imports needed for the solution:
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties

chart1 = LineChart()
# setup and append the first series
values = Reference(dprsheet, min_col=5, min_row=4, max_row=34)
series = Series(values, title="Series 1")
chart1.append(series)
# setup and append the second series
values = Reference(dprsheet, min_col=8, min_row=4, max_row=34)
series = Series(values, title="Series 2")
chart1.append(series)

dates = Reference(dprsheet, min_col=2, min_row=4, max_row=34)
chart1.set_categories(dates)

#create label styling
axis = CharacterProperties(sz=800)
rot = openpyxl.drawing.text.RichTextProperties(vert='vert270')

#set axis label styles
chart1.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)], bodyPr=rot)
chart1.y_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)])

#set data labels and styles
s1 = chart1.series[0]
s1.dLbls = DataLabelList() 
s1.dLbls.showVal = True
s1.dLbls.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)], bodyPr=rot)

注意chart.dataLabels.dLblPos还是不行(txPr可以修改但不能修改position)。明显的解决方法需要分别设置每个系列的位置。