如何在openpyxl中指定饼图楔形颜色?

时间:2018-12-20 21:54:30

标签: python excel openpyxl

我没有在openpyxl文档中看到有关如何指定饼图的楔形颜色的示例。

我在文档中找到了这个: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.drawing.fill.html?highlight=openpyxl.drawing.fill#openpyxl.drawing.fill.SolidColorFillProperties

这个例子利用了类似的东西: https://openpyxl.readthedocs.io/en/latest/charts/pattern.html?highlight=ColorChoice

1 个答案:

答案 0 :(得分:0)

解决方案:

from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference
from openpyxl.chart.marker import DataPoint

wb = Workbook()
ws = wb.active

rows = [
    ("Sample",),
    (1,),
    (2,),
    (3,),
    (2,),
    (3,),
    (3,),
    (1,),
    (2,),
]

for r in rows:
    ws.append(r)

p = PieChart()
data = Reference(ws, min_col=1, min_row=1, max_row=8)
p.add_data(data, titles_from_data=True)
p.title = "Sample Chart"

# try to set color blue (0000FF) for the 2nd wedge (idx=1) in the series
series = p.series[0]
pt = DataPoint(idx=1)
pt.graphicalProperties.solidFill = "0000FF"
series.dPt.append(pt)

ws.add_chart(p, "C1")
wb.save("pattern.xlsx")