使用 Python PPTX 将数据表添加到折线图
Adding Data tables to line charts with Python PPTX
我收到了创建折线图的请求,该折线图包括自动生成的带有图例的数据表。这在 PowerPoint 中目前是可能的:
Scanny 确认这不受支持,所以现在我尝试直接插入 XML。这是我需要插入的内容:
<c:dTable><c:showHorzBorder val="1"/><c:showVertBorder val="1"/><c:showOutline val="1"/><c:showKeys val="1"/></c:dTable>
它恰好位于结束标记之前。我已经想出如何编辑 XML 更精细的细节,但在此位置插入新内容是新的。
也许你想考虑使用 COM,然后你可以有一个带有图表的模板 pptx,其中数据 table 已经在图例中启用。然后只需替换数据,图例就会在您 re-fresh 图表时自行更新。更新 xml 对我来说似乎是一项艰巨的任务 :)。或者(您仍然需要一个带有图表的模板,其中启用了带有数据 table 的图例选项-> 您可以替换 python-pptx 中的数据,但我几乎可以肯定您需要刷新图表为了更新图例)
好的,经过广泛搜索后,我找到了一个 ,它正在访问 XML,这与我正在寻找的大致相同。这有助于更好地理解我需要做什么。基于该解决方案,我将以下内容添加到我的代码中,该代码在调用时有效!
定义图表:
graphic_frame = placeholder.insert_chart(v.charttypelist[intendedchart], chart_data)
chart = graphic_frame.chart
方法:
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')
我收到了创建折线图的请求,该折线图包括自动生成的带有图例的数据表。这在 PowerPoint 中目前是可能的:
Scanny 确认这不受支持,所以现在我尝试直接插入 XML。这是我需要插入的内容:
<c:dTable><c:showHorzBorder val="1"/><c:showVertBorder val="1"/><c:showOutline val="1"/><c:showKeys val="1"/></c:dTable>
它恰好位于结束标记之前。我已经想出如何编辑 XML 更精细的细节,但在此位置插入新内容是新的。
也许你想考虑使用 COM,然后你可以有一个带有图表的模板 pptx,其中数据 table 已经在图例中启用。然后只需替换数据,图例就会在您 re-fresh 图表时自行更新。更新 xml 对我来说似乎是一项艰巨的任务 :)。或者(您仍然需要一个带有图表的模板,其中启用了带有数据 table 的图例选项-> 您可以替换 python-pptx 中的数据,但我几乎可以肯定您需要刷新图表为了更新图例)
好的,经过广泛搜索后,我找到了一个
定义图表:
graphic_frame = placeholder.insert_chart(v.charttypelist[intendedchart], chart_data)
chart = graphic_frame.chart
方法:
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')