Adding Data tables to line charts with Python PPTX

631 Views Asked by At

I've received a request to create line charts that include automatically-generated data tables with the legend. This is currently possible in PowerPoint:

PowerPoint Screenshot PowerPoint Chart

Scanny confirmed this isn't supported, so now I'm trying to insert the XML directly. Here's what I need to insert:

<c:dTable><c:showHorzBorder val="1"/><c:showVertBorder val="1"/><c:showOutline val="1"/><c:showKeys val="1"/></c:dTable>

It falls right before the closing tag </c:plotArea>. I've figured out how to edit XML on more granular details, but inserting a new thing in this location is something new.

2

There are 2 best solutions below

0
On BEST ANSWER

Ok, so after some extensive searching, I found a question about changing the size of the plot area, which is accessing XML in the ballpark of what I was looking for. This helped be better understand what I needed to do. Based on that solution, I added the following to my code which works when called!

Defining chart:

graphic_frame = placeholder.insert_chart(v.charttypelist[intendedchart], chart_data)
chart = graphic_frame.chart

The method:

def SubElement(parent, tagname, **kwargs):
    element = OxmlElement(tagname)
    element.attrib.update(kwargs)
    parent.append(element)
    return element

def add_table_line_graph(chart):
    plotArea = chart._element.chart.plotArea
    SubElement(plotArea, 'c:dTable') # Add data table tag
    dataTable = chart._element.chart.plotArea.dTable
    # Add values to data table tag
    for sub in ['c:showHorzBorder', 'c:showVertBorder', 'c:showOutline', 'c:showKeys']:
        SubElement(dataTable, sub, val='1')
3
On

Maybe you want to consider using COM then you could have a template pptx with the Chart where the data table is already enabled in the legend. Then it would simply be a matter of substituting the data and the legend will update itself when you re-fresh the chart. Updating the xml seems like a daunting task to me :). Alternatively (you stil need a template with the chart where the option for legend with data table is enabled-> you can substitute the data in python-pptx but then I am almost sure you need to do a chart refresh in order for the legend to be updated)