如何使用Python在现有Powerpoint中定义、提取和替换图表中的数据

如何使用Python在现有Powerpoint中定义、提取和替换图表中的数据,python,powerpoint,python-pptx,Python,Powerpoint,Python Pptx,目前我正在使用以下代码来定义和替换 现有Powerpoint演示文稿中的占位符(文本数据) current_dir = os.path.dirname(os.path.realpath(__file__)) prs = Presentation(current_dir + '/test2.pptx') slides = prs.slides title_slide_layout = prs.slide_layouts[0] slide = slides[0] for shape in sl

目前我正在使用以下代码来定义和替换 现有Powerpoint演示文稿中的占位符(文本数据)

current_dir = os.path.dirname(os.path.realpath(__file__))

prs = Presentation(current_dir + '/test2.pptx')

slides = prs.slides

title_slide_layout = prs.slide_layouts[0]
slide = slides[0]
for shape in slide.placeholders:
    print('%d %s' % (shape.placeholder_format.idx, shape.name))
title = slide.shapes.title
subtitle1 = slide.shapes.placeholders[0]
subtitle2 = slide.shapes.placeholders[10]
subtitle10 = slide.shapes.placeholders[11]
subtitle11 = slide.shapes.placeholders[12]

subtitle1.text = "1"
subtitle2.text = "2"
subtitle10.text = "3"
subtitle11.text = "4"


slide2 = slides[1]
for shape in slide2.placeholders:
    print('%d %s' % (shape.placeholder_format.idx, shape.name))
subtitle3 = slide2.shapes.placeholders[10]
subtitle4 = slide2.shapes.placeholders[11]
subtitle5 = slide2.shapes.placeholders[12]
subtitle6 = slide2.shapes.placeholders[13]
subtitle12 = slide2.shapes.placeholders[16]
companydate = slide2.shapes.placeholders[14]

subtitle3.text = "1"
subtitle4.text = "2"
subtitle5.text = "3"
subtitle6.text = "4"
subtitle12.text = "40%"
companydate.text = "Insert company"

slide3 = slides[2]
for shape in slide3.placeholders:
     print('%d %s' % (shape.placeholder_format.idx, shape.name))
subtitle7 = slide3.shapes.placeholders[10]
subtitle8 = slide3.shapes.placeholders[11]
subtitle9 = slide3.shapes.placeholders[12]
subtitle13 = slide3.shapes.placeholders[16]
companydate2 = slide3.shapes.placeholders[14]

subtitle7.text = "1"
subtitle8.text = "2"
subtitle9.text = "3"
subtitle13.text = "5x"
companydate2.text = "Insert Company"

slide4 = slides[3]
# for shape in slide4.placeholders:
#print('%d %s' % (shape.placeholder_format.idx, shape.name))
companydate3 = slide4.shapes.placeholders[14]
companydate3.text = "Insert Company"

"'Adapting Charts'"
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Pt

"Adapting Chart 1"

prs1 = Presentation(current_dir + '/output4.pptx')
slides1 = prs1.slides

chart1 = prs1.slides[0].chart
然而,我也在后台运行分析,我想知道是否有可能在提取和替换这些图表中的数据的同时识别(定义)同一演示文稿中的图表。这些字符不嵌入模板中。 由于使用plotly或mathplotlib绘制图表不会呈现符合要求的图像,除非完全修改为以下格式,否则我无法使用这些图像: 如果是,是否可以给出具体的编码示例


提前谢谢

是的,这是可能的。文档将是您的最佳来源

这将找到图表形状:

for shape in slide.shapes:
    if shape.has_chart:
        chart = shape.chart
        print('found a chart')
从图表系列中提取数据:

通过创建新的ChartData对象并使用该图表数据对象在图表上调用
。replace_Data()
,可以替换数据:

chart_data = ChartData(...)
...  # add categories, series with values, etc.
chart.replace_data(chart_data)

除了上面@scanny的答案之外,这对我很有用:

    if shape.name == 'Chart1':
    chart = shape.chart
    print(shape.name)

    for series in chart.plots:
        print(list(series.categories))
        cat = list(series.categories)

    for series in chart.series:
        ser = series.values
        print(series.values)

    try:    
    # ---define new chart data---
        chart_data = CategoryChartData()
        chart_data.categories = cat
        chart_data.add_series('category', df['column'])
        # ---replace chart data---
        chart.replace_data(chart_data)
    except KeyError:
        continue
使用上述代码,您可以打印类别和系列值,然后用新值替换它们(同时保持类别不变)


我添加了KeyError异常,因为如果没有它,您将得到一个“rId3”错误。从论坛上看,似乎在写PPTX时存在一些XML编写问题。

嗨,Scanny,非常感谢你的回答,我会尽快试用并告诉你结果。@Scanny有没有办法打印图表的所有分类数据?它应该打印图表的所有参数。
    if shape.name == 'Chart1':
    chart = shape.chart
    print(shape.name)

    for series in chart.plots:
        print(list(series.categories))
        cat = list(series.categories)

    for series in chart.series:
        ser = series.values
        print(series.values)

    try:    
    # ---define new chart data---
        chart_data = CategoryChartData()
        chart_data.categories = cat
        chart_data.add_series('category', df['column'])
        # ---replace chart data---
        chart.replace_data(chart_data)
    except KeyError:
        continue