【问题标题】:How to set Background Color of Plot Area of Chart using openpyxl如何使用openpyxl设置图表绘图区域的背景颜色
【发布时间】:2017-04-10 17:42:40
【问题描述】:
我想使用 openpyxl 更改图表的背景颜色 as in this example。
在一次google群讨论中我发现了以下代码sn-p:
from openpyxl.chart.shapes import GraphicalProperties
props = GraphicalProperties(solidFill="999999")
chart.graphical_properties = props
chart.plot_area.graphical_properties = props
但保存到excel文件时对图表没有任何影响。
【问题讨论】:
标签:
python
charts
styling
openpyxl
【解决方案1】:
这个功能在之前的 openpyxl 版本中似乎被破坏了,并在 2.4.7 版中得到修复。要达到图片所示的效果,您需要更改plot_area 的纯色填充颜色:
from openpyxl import Workbook
from openpyxl.chart import BarChart
from openpyxl.chart.shapes import GraphicalProperties
wb = Workbook()
ws = wb.active
chart = BarChart()
props = GraphicalProperties(solidFill="999999")
chart.plot_area.graphicalProperties = props
ws.add_chart(chart, "A1")
wb.save("bar.xlsx")
请注意:拥有chart 图形属性的成员对象是chart.graphical_properties,而在plot_area 中它被命名为plot_area.graphicalProperties - 它本身就是plot_area.spPr 的别名。
您需要确保访问正确的成员以创建一个有效的数据结构,该结构与您在 Excel 文件中的预期相同。