首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在openpyxl中格式化图表数据标签

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

Stack Overflow用户
提问于 2017-11-29 10:29:25
回答 2查看 6K关注 0票数 1

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

代码语言:javascript
运行
复制
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')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-05 10:46:06

我最终到了那里,我试着想出如何改变轴标.这应该扩展到任何更改(使用字体等的段落属性或对齐的主体属性等)。

代码语言:javascript
运行
复制
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可以修改,但不能修改位置)。显而易见的工作需要在每个系列上单独设置位置。

票数 2
EN

Stack Overflow用户

发布于 2017-11-29 12:29:28

对不起,但恐怕您遇到了OOXML规范的限制,而OOXML规范在这方面并不总是很清楚。本质上,openpyxl在这个级别将XML模式公开给Python,所以最好将规范的副本与Excel创建的类似文件一起交给,看看它能做什么。

举个例子,这类东西是没有文档的:如果你想要图表线有不同的颜色,你必须在lumOff之前在lumMod之前在DrawingML的相关部分。

我们永远无法在openpyxl中记录这一切。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47550555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档