Python, pptx to change line color in line chart
Python, pptx to change line color in line chart
Created a line chart using 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.
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')
@NitinPawar, yes, I both did.
– Mark K
Aug 21 at 8:23
1 Answer
1
Use series.format.line instead of series.format.fill:
https://python-pptx.readthedocs.io/en/latest/api/dml.html#pptx.dml.chtfmt.ChartFormat
series.format.line
series.format.fill
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
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you tried altering the RGBColor parameters further, and deleting the file before running the script again?
– Nitin Pawar
Aug 21 at 8:20