Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在openpyxl中格式化图表数据标签_Python_Openpyxl - Fatal编程技术网

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

Python 在openpyxl中格式化图表数据标签,python,openpyxl,Python,Openpyxl,我正在使用Python3.6.3使用openpyxl(2.4.9)编写一些excel工作表。在图表数据上获取数据标签并不明显,但当我尝试格式化所述数据标签时,事情开始变得糟糕。我想做的是改变他们的位置,也改变他们的旋转。有人有什么想法吗?我对python有点陌生,所以任何建议都很好。 以下是我尝试过的: from openpyxl.chart import LineChart, Reference, Series from openpyxl.chart.label import DataLabe

我正在使用Python3.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规范的局限性,而OOXML规范在这方面并不总是非常清楚。本质上,在这个级别上,openpyxl向Python公开了XML模式,因此您最好手头有一份规范的副本,以及一个由Excel创建的可比较的文件,以查看它的功能

例如,未记录的内容:如果希望图表线具有不同的颜色,则必须在DrawingML的相关部分中的
lumOff
之前添加
lumMod


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

我最终通过尝试解决如何更改轴标签实现了这一点。。。这应该可以扩展到任何更改(使用字体等的段落属性或对齐等的正文属性)

请注意,chart.dataLabels.dLblPos仍然不起作用(txPr可以修改,但不能修改位置)。显而易见的解决方案要求在每个系列上单独设置位置

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)