简体   繁体   English

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

[英]Python, pptx to change line color in line chart

Created a line chart using pptx. 使用pptx创建折线图。 I want to change the line color to red. 我想将线条颜色更改为红色。

Added the "fill" and indicated the RGBColor (255, 0, 0) but it's still blue. 添加了“填充”并指示RGBColor(255、0、0),但仍为蓝色。

How can I change it to red? 如何将其更改为红色? Thank you. 谢谢。

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')

Use series.format.line instead of series.format.fill : 使用series.format.line而不是series.format.fill
https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.chtfmt.ChartFormat https://python-pptx.readthedocs.io/zh-CN/latest/api/dml.html#pptx.dml.chtfmt.ChartFormat

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

The LineFormat object can also be used to set line style (dashed, dotted, etc.) and line width: https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.line.LineFormat LineFormat对象还可以用于设置线型(虚线,点线等)和线宽: https : //python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.line .LineFormat

The lines in a line chart each have a line, but not a fill (as would say a bar-chart). 折线图中的每条线都有一条线,但没有填充线(可以说是条形图)。 In a bar-chart, you can set both line and fill, separately. 在条形图中,可以分别设置线条和填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM