gpt4 book ai didi

python ppt在图表中查找和替换

转载 作者:行者123 更新时间:2023-12-05 04:26:46 26 4
gpt4 key购买 nike

我已经在此处引用了这些帖子 here , herehere .请不要将其标记为重复。

我在ppt中嵌入了一个图表,如下所示

enter image description here

我希望将轴头从 FY2021 HC 替换为 FY1918 HC。同样,FY2122 HC 应替换为 FY1718 HC

如何使用 python pptx 执行此操作?不过,此图表来自嵌入式 Excel。 ppt里有没有办法改?

当我尝试下面的方法时,它没有得到轴标题

text_runs = []
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
text_runs.append(run.text)

当我执行以下操作时,我从特定幻灯片中找到了形状类型列表。我只想更改图表标题。因此,屏幕截图仅显示了我幻灯片中的两个图表。

for slide in ip_ppt.slides:
for shape in slide.shapes:
print("id: %s, type: %s" % (shape.shape_id, shape.shape_type))

id: 24, type: TEXT_BOX (17)
id: 10242, type: TEXT_BOX (17)
id: 11306, type: TEXT_BOX (17)
id: 11, type: AUTO_SHAPE (1)
id: 5, type: TABLE (19)
id: 7, type: TABLE (19)
id: 19, type: AUTO_SHAPE (1)
id: 13, type: CHART (3)
id: 14, type: CHART (3)

当我尝试使用 id 访问形状时,我也无法访问

ip_ppt.slides[5].shapes[13].Chart

我也试过下面的代码

from pptx import chart
from pptx.chart.data import CategoryChartData
chart_data = CategoryChartData()
chart.datalabel = ['FY1918 HC', 'FY1718 HC']

我是 python 和 pptx 的新手。任何关于如何编辑嵌入图表标题的解决方案都非常有用。请帮忙

最佳答案

您可以通过以下方式获取类别标签:

from pptx import Presentation
from pptx.shapes.graphfrm import GraphicFrame

prs = Presentation('chart-01.pptx')

for slide in prs.slides:
for shape in slide.shapes:
print("slide: %s, id: %s, index: %s, type: %s" % (slide.slide_id, shape.shape_id, slide.shapes.index(shape), shape.shape_type))
if isinstance(shape, GraphicFrame) and shape.has_chart:
plotIndex = 0
for plot in shape.chart.plots:
catIndex = 0
for cat in plot.categories:
print(" plot %s, category %s, category label: %s" % (plotIndex, catIndex, cat.label))
catIndex += 1
plotIndex += 1

它会输出类似的东西:

slide: 256, id: 2, index: 0, type: PLACEHOLDER (14)
slide: 256, id: 3, index: 1, type: CHART (3)
plot 0, category 0, category label: East
plot 0, category 1, category label: West
plot 0, category 2, category label: Midwest

很遗憾,您无法更改类别标签,因为它存储在嵌入式 Excel 中。更改这些的唯一方法是使用 chart.replace_data() 方法替换图表数据。

根据现有图表重新创建调用 replace_data 所需的 ChartData 对象有点复杂,但这是我根据使用以下代码创建的图表进行的操作:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE,XL_LABEL_POSITION
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# define chart data ---------------------
chart_data = CategoryChartData()
chart_data.categories = ['FY2021 HC', 'FY2122 HC']
chart_data.add_series('blue', (34.5, 31.5))
chart_data.add_series('orange', (74.1, 77.8))
chart_data.add_series('grey', (56.3, 57.3))

# add chart to slide --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
gframe = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_STACKED, x, y, cx, cy, chart_data
)
chart = gframe.chart
plot = chart.plots[0]
plot.has_data_labels = True
data_labels = plot.data_labels
data_labels.font.size = Pt(13)
data_labels.font.color.rgb = RGBColor(0x0A, 0x42, 0x80)
data_labels.position = XL_LABEL_POSITION.INSIDE_END

prs.save('chart-01.pptx')

这看起来与您在问题中的图片几乎相同:

The view inside PowerPoint of the file chart-01.pptx

以下代码将更改该图表中的类别标签:

from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.shapes.graphfrm import GraphicFrame
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches

# read presentation from file
prs = Presentation('chart-01.pptx')

# find the first chart object in the presentation
slideIdx = 0
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_chart:
chart = shape.chart
print("Chart of type %s found in slide[%s, id=%s] shape[%s, id=%s, type=%s]"
% (chart.chart_type, slideIdx, slide.slide_id,
slide.shapes.index(shape), shape.shape_id, shape.shape_type ))
break
slideIdx += 1

# create list with changed category names
categorie_map = { 'FY2021 HC': 'FY1918 HC', 'FY2122 HC': 'FY1718 HC' }
new_categories = list(categorie_map[c] for c in chart.plots[0].categories)

# build new chart data with new category names and old data values
new_chart_data = CategoryChartData()
new_chart_data.categories = new_categories
for series in chart.series:
new_chart_data.add_series(series.name,series.values)

# write the new chart data to the chart
chart.replace_data(new_chart_data)

# save everything in a new file
prs.save('chart-02.pptx')

评论应该解释发生了什么,如果你用 PowerPoint 打开 chart-02.pptx,你会看到:

The view inside PowerPoint of the file chart-02.pptx

希望能解决您的问题!

关于python ppt在图表中查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72949464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com