使用 python-pptx 从现有图表中读取图表数据

Var*_*bar 4 python python-pptx

我想使用 python-pptx 库从演示文稿中的图表中读取数据。我已经看过替换图表数据的文档,但我不知道如何读取数据。

sca*_*nny 5

图表数据为:

  • 图表的图表类型
  • 它的类别名称(以及可能的层次结构)
  • 它的系列名称
  • 及其系列值

这些可在绘图级别使用,例如:

>>> chart.chart_type
DOUGHNUT
>>> plot = chart.plots[0]
>>> category_labels = [c.label for c in plot.categories]
["West", "North", "South"]
>>> series = plot.series[0]
>>> series.name
'Sales'
>>> series.values
(17.8, 24.5, 88.0)
Run Code Online (Sandbox Code Playgroud)

根据图表/绘图类型的不同,存在一些微妙之处。请参阅此处的 API 文档以了解有关这些内容的更多信息。 https://python-pptx.readthedocs.io/en/latest/api/chart.html