scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 520 forks source link

Marker border color in XL_CHART_TYPE.LINE_MARKERS #872

Open DanielvdC opened 1 year ago

DanielvdC commented 1 year ago

Hi,

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?

I very much enjoy working with Python PPTX, so hopefully somebody can help me out!

A 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: image