Search code examples
python-pptx

Hiding a chart series in python-pptx


Using python-pptx, how can I create a new chart populated with multiple series of data but only show some of those series visibly on the chart? The other series should be in the background data of the chart but not visible when the slide is presented (see image 1 for clarification).

Image 1: Selection of columns that are to be shown initially

The intention is that I have multiple types of measurements (percentage, absolute, index) for the same slide. At first I only want to show the percentage values on the chart. If the chart does not look nice, I want to be able to rightclick on the chart and select other columns with other measurements to be shown (see image 2).

Image 2: Newly selected columns that were previously hidden

A workaround would be to create hidden slides for each additional measurement and then to activate them if needed. But that does not work for my case - the data must be all in one slide like in the examples.

Looking forward to reading your answers! :)

current test code:

prs = Presentation("MyPPTX.pptx")
chart_layout = prs.slide_layouts[11]
chart_placeholder = prs.slides.add_slide(chart_layout).placeholders[15]

# define chart data ---------------------
chart_data_prz = ChartData()
chart_data_prz.categories = ['Male', 'Female']
chart_data_prz.add_series('Age_18-34_in_%', (51,49))
chart_data_prz.add_series('Video-Streaming_in_%', (58, 42))
chart_data_prz.add_series('Age_18-34_in_Mio', (105, 96))
chart_data_prz.add_series('Video-Streaming_in_Mio', (118, 83))
prz_chart = chart_placeholder.insert_chart(XL_CHART_TYPE.BAR_CLUSTERED,chart_data_prz)

Solution

  • There is no API support for this in the current version of python-pptx (v0.6.17).

    The embedded Excel workbook providing the chart data is derived from the same ChartData object the chart is; that is, the only way to get data into the chart's Excel workbook with python-pptx is to put it in the ChartData object used to create the chart, and python-pptx adds all those series to the chart (which makes them visible).