Search code examples
python-pptx

How to change marker border colors in XL_CHART_TYPE.LINE_MARKERS python-pptx


I want to create a line chart with markers, where every line has customized markers. I can modify the color of the lines and the style of the markers. However, when I try to update the color of the markers, I can only change the inner color, excluding the border. I can't seem to find any information about the marker border, so am I missing something or is this a bug?

Minimal example:

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.chart import XL_CHART_TYPE, XL_MARKER_STYLE
from pptx.dml.color import RGBColor
from pptx.chart.data import ChartData

root = Presentation()
  
# Creating slide layout
first_slide_layout = root.slide_layouts[0] 

slide = root.slides.add_slide(first_slide_layout)


# Create new chart
chart_data = ChartData()
# X-axis labels
chart_data.categories = ['2023','24','25','2026']

series = {'Planned':[0,10,20,30],
          'Forecasted':[10,50,5,20]}

# Loop over series and create data
for s in series:
    chart_data.add_series(s, series[s])
    
# Add data to chart
chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE_MARKERS,
                              Cm(5), Cm(5), Cm(20), Cm(10),
                              chart_data).chart

    
# Format lines
# Fetch data from the created chart
plot = chart.plots[0]
# Enumerate over series in data
for i,s in enumerate(plot.series):
    # Get line data
    line = s.format.line
    # Format line
    if i == 0:
        # Line color
        line.color.rgb = RGBColor(0,0,0)
        # Marker color
        s.marker.style = XL_MARKER_STYLE.CIRCLE
        fill = s.marker.format.fill
        fill.solid()
        fill.fore_color.rgb = RGBColor(0,0,0)
        
    if i == 1:
        # Line color
        line.color.rgb = RGBColor(125,125,125)
        # Marker color
        s.marker.style = XL_MARKER_STYLE.DIAMOND
        s.marker.size = 12
        fill = s.marker.format.fill
        fill.solid()
        fill.fore_color.rgb = RGBColor(125,125,125)
        
root.save(r'C:\Users\XX\Desktop\PPTX_example.pptx')

Output figure

Tried to fill using a solid fill and then assign RGBColor, but this does not color the border of the marker.


Solution

  • Is that what you mean? (accessing and setting the markers fill to solid and then colouring it)

    from pptx import Presentation
    from pptx.util import Cm, Pt
    from pptx.enum.chart import XL_CHART_TYPE, XL_MARKER_STYLE
    from pptx.dml.color import RGBColor
    from pptx.chart.data import ChartData
    
    root = Presentation()
      
    # Creating slide layout
    first_slide_layout = root.slide_layouts[0] 
    
    slide = root.slides.add_slide(first_slide_layout)
    
    
    # Create new chart
    chart_data = ChartData()
    # X-axis labels
    chart_data.categories = ['2023','24','25','2026']
    
    series = {'Planned':[0,10,20,30],
              'Forecasted':[10,50,5,20]}
    
    # Loop over series and create data
    for s in series:
        chart_data.add_series(s, series[s])
        
    # Add data to chart
    chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE_MARKERS,
                                  Cm(5), Cm(5), Cm(20), Cm(10),
                                  chart_data).chart
    
        
    # Format lines
    # Fetch data from the created chart
    plot = chart.plots[0]
    # Enumerate over series in data
    for i,s in enumerate(plot.series):
        # Get line data
        line = s.format.line
        # Format line
        if i == 0:
            # Line color
            line.color.rgb = RGBColor(0,0,0)
            # Marker color
            s.marker.style = XL_MARKER_STYLE.CIRCLE
            fill = s.marker.format.fill
            fill.solid()
            fill.fore_color.rgb = RGBColor(0,0,0)
            fillline = s.marker.format.line.fill
            fillline.solid()
            fillline.fore_color.rgb = RGBColor(0,255,0)
            
        if i == 1:
            # Line color
            line.color.rgb = RGBColor(125,125,125)
            # Marker color
            s.marker.style = XL_MARKER_STYLE.DIAMOND
            s.marker.size = 12
            fill = s.marker.format.fill
            fill.solid()
            fill.fore_color.rgb = RGBColor(125,125,125)
            fillline = s.marker.format.line.fill
            fillline.solid()
            fillline.fore_color.rgb = RGBColor(0,255,0)
            
    root.save('test.pptx')