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

2023-12-10

我正在使用 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')

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

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 可以修改,但位置不能修改)。明显的解决方法需要在每个系列上单独设置位置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 openpyxl 中格式化图表数据标签 的相关文章

随机推荐

  • 从 _Layout.cshtml 调用部分视图

    我有一个主类别 主类别在数据库中有其子类别列表 所以它是一个关系数据库 为了更好地理解 附上了 edmx 地图图片 现在我的目标是一个名为 GuestNav cshtml 的部分视图将包含类别和子类别列表显示 然后从 Layout csht
  • Rails 3:Ajax 使用部分更新视图

    我对以下 Ajax 请求做错了什么 链接 调用 路线 match it business analysis gt informationtechnology business analysis as gt it business analy
  • 如何让irb和rails控制台在gitbash中正常工作?

    我在 Windows 7 上使用 gitbash 版本 2 9 0 64 位 它使用 mintty 版本 2 0 3 大多数时候 gitbash shell 似乎工作正常 您可以使用箭头键等 就像使用任何 bash shell 一样 它们会
  • 如何使用 python 访问 Azure AD 组列表?

    我是蟒蛇新手 我找到了以下示例代码来检索 Azure AD 组https msdn microsoft com en us Library Azure Ad Graph api groups operations Basicoperatio
  • 比较Java中的浮点数[重复]

    这个问题在这里已经有答案了 可能的重复 在java中操作和比较浮点数 在 Java 中比较浮点数时我应该使用 Epsilon 吗 Float compare float f1 float f2 静态方法可以安全使用吗 Thanks 编辑 我
  • 视图对聚合函数与结果集限制的性能影响

    问题 Using PostgreSQL 13 我遇到了一个性能问题 根据我执行的 select 语句 从连接两个表的视图中选择最高 id 这是一个示例设置 CREATE TABLE test1 id BIGSERIAL PRIMARY KE
  • 使用 MQTT 代理对 ESP8266 Wemos D1 Mini 进行 SSL 证书验证

    我有一个树莓派 3 其操作系统为 raspbianstretch 我已经按照本教程在树莓派上安装并完全配置了 MQTT 代理 https www digitalocean com community tutorials how to ins
  • 将基于度数的地理坐标与正则表达式进行匹配

    我希望能够识别表单的模式 28 44 30 N 33 12 36 E 这是我到目前为止所拥有的 use utf8 qr d 1 3 s s d 1 2 s s d 1 2 s s ENSW s s s 2 x 不用说 这不匹配 和扩展字符
  • JavaScript 关键字“with”真的被弃用了吗?

    使用JavaScriptwith长期以来 语句一直被认为是不好的编码实践 并被建议不要这样做 这一页表明with已被弃用 并且在严格模式下会抛出错误 我的浏览器在使用时会抛出错误with在严格模式下 Google Closure Compi
  • 动态创建的脚本不会阻止渲染?

    在本文中 https www html5rocks com en tutorials speed script loading 他们在说 动态创建并添加到文档中的脚本默认是异步的 它们不会阻止渲染 但 执行javascript 总是阻塞渲染
  • 如何对 URL 的西里尔字符进行编码然后解码?

    我在一页上有一个表格
  • 根据 Java 中的 Swagger 定义验证 JSON 消息

    我使用 Swagger 定义创建了一个 REST API 现在我需要使用该 swagger 架构验证传入消息 我找到了几种解决方案 但它们都依赖于某些特定的用例 最接近我需要的是this正如它的描述所示 它与提供的 json 模式配合得很好
  • 无法上传 apk:READ_EXTERNAL_STORAGE 具有不同的 maxSdkVersions

    这是我第一次在这里提问 不管怎样 正如标题所说 我正在尝试将我的应用程序推向市场 当我尝试上传签名的 apk 时 出现错误 上传失败具有不同 maxSdkVersions 的重复权限声明 android permission READ EX
  • Facebook App Invites iOS v4 SDK 错误

    我已经仔细检查了所有内容 根据我的理解 这就是我需要做的 self inviteContent FBSDKAppInviteContent alloc initWithAppLinkURL NSURL URLWithString http
  • Asterisk-如何使用 ChanSpy() 或任何替代方法“耳语”音乐? [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 我打算向其中一位会议参与者 耳语 音乐 消息文件 而不让其他会议参与者听到此消息 我浏览了 Asterisk Cookbook 中的音频操作章节 http ofps oreill
  • 如何在 Objective-C iphone sdk 中迭代嵌套字典

    您好 我有一个 json 字符串 已通过 JSON 框架转换为字典 我需要提取其内容 我怎样才能迭代到嵌套字典 我已经有了这段代码 可以让我查看字典 NSDictionary results responseString JSONValue
  • 如何使用 nav.popTo() (ionic 2)?

    我正在使用导航控制器 要返回 我可以使用 nav pop 但是如果我需要转到其他页面 不是最后一个页面 如何使用 nav popTo constructor nav NavController this nav nav this nav p
  • Numpy int 位长度

    我想找到以二进制表示无符号 numpy 整数 或整数数组中的每个元素 所需的位数 就像 python 的int bit length 确实如此 但 numpy 似乎没有等效的功能 例如 gt gt gt int 0b1000 bit len
  • 如何防止 Dockerfile 缓存 git clone

    我有一个 Dockerfile 试图将 Web 应用程序打包并部署到容器中 应用程序的代码在 Docker 镜像构建期间从 git 存储库获取 这是 Dockerfile 快照 RUN git clone depth 1 git repos
  • 在 openpyxl 中格式化图表数据标签

    我正在使用 Python 3 6 3 使用 openpyxl 2 4 9 编写一些 Excel 工作表 在图表数据上获取数据标签并不明显 但当我尝试格式化所述数据标签时 事情开始变得糟糕 我想要做的是改变他们的位置并改变他们的轮换 有人有什