Python, pptx 改变折线图中的线条颜色

Python, pptx to change line color in line chart

使用 pptx 创建了折线图。我想把线条颜色改成红色。

添加了 "fill" 并指示了 RGBColor (255, 0, 0) 但它仍然是蓝色。

如何将其更改为红色?谢谢。

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from pptx.dml.color import RGBColor

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

chart_data = ChartData()
chart_data.categories = ["2014", "2015", "2016", "2017", "2018"]
chart_data.add_series('Series 1', ("50", "45", "46", "52", "56"))

x, y, cx, cy = Inches(0.5), Inches(2), Inches(8), Inches(3.5)

graphic_frame = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data)
chart = graphic_frame.chart
plot = chart.plots[0]
series = plot.series[0]

fill = series.format.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 0, 0)

prs.save('c:\My Documents\line chart.pptx')

使用series.format.line代替series.format.fill
https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.chtfmt.ChartFormat

line = series.format.line
line.color.rgb = RGBColor(255, 0, 0)

LineFormat对象还可以用来设置线型(虚线、点线等)和线宽:https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.line.LineFormat

折线图中的每条线都有一条线,但没有填充(就像条形图一样)。在条形图中,您可以分别设置线条和填充。