Chart Losing formatting when changing the data with python-pptx

83 views Asked by At

I have a pptx chart with lots of customization. Every day I want to generate a dataframe in pandas and replace the chart data with the new dataframe data. What is happening is that the formatting specific to each series is getting messed up, sometimes they just change between the series (Series B formatting goes to Series A and vice versa), and sometimes the just change randomly.

What I tried doing was:

from pptx import Presentation

ppt = Presentation('path/to/presentation.pptx')

series_order = []
for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_chart:
            chart = shape.chart
            for series in chart.series:
                series_order.append(series.name)


for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_chart:
            chart = shape.chart

            data = CategoryChartData()

            for col in series_order:
                data.add_series(col, df[col].tolist())

            data.categories = df[df.columns['categories']].tolist()

            chart.replace_data(data)

and this is one of the effects I got using this method: Original Chart Resulting Chart

0

There are 0 answers