2

目前,我正在使用以下代码来定义和替换现有 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 绘制图表不会呈现符合要求的图像,我无法使用这些,除非完全修改为以下格式:图表预算 单击相关 如果是,是否可以给出具体的编码示例?

提前致谢!

4

2 回答 2

3

Yes, it's possible to do that. The documentation will be your best source.

This will find the chart shapes:

for shape in slide.shapes:
    if shape.has_chart:
        chart = shape.chart
        print('found a chart')

Data is extracted from the chart series(es):

for series in chart.series:
    for value in series.values:
        print(value)

Data is replaced by creating a new ChartData object and calling .replace_data() on the chart using that chart data object:

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

http://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.chart.Chart.replace_data

于 2017-06-21T19:53:26.930 回答
0

添加到上面@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 写入问题。

于 2020-06-06T21:34:25.397 回答