You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Python PPTX代码:让新增幻灯片紧跟Advantages幻灯片

解决Python PPTX插入幻灯片位置问题

我使用Python PPTX库向PPT幻灯片粘贴文本,现有代码可将text_into_slide变量中的文本以要点形式粘贴至标题为Advantages的幻灯片。当要点数量为5-6个时无问题,但通常我有约15个要点。需求为:将前5个要点粘贴至Advantages幻灯片,若要点超过5个,则每5个要点新建一张幻灯片并粘贴内容。当前代码可实现该功能,但新建幻灯片会被添加至文档末尾,而非紧跟Advantages幻灯片(该幻灯片位置不固定,依项目不同可能为第6、8或10张等),需要修改代码使剩余要点对应的幻灯片直接插入到Advantages幻灯片之后。

现有代码

from pptx import Presentation
from pptx.util import Pt
from pptx.enum.text import PP_ALIGN

# Load the PowerPoint presentation
pptx_file = "23102023_Presentation.pptx"
presentation = Presentation(pptx_file)

# Search for the slide titled "Advantages"
target_slide_title = "Advantages"
found_slide = None

for i, slide in enumerate(presentation.slides):
    if slide.shapes.title.text == target_slide_title:
        found_slide = slide
        found_slide_index = i
        break

# Check if the slide was found
if found_slide is not None:
    # Define the text to insert
    text_into_slide_text = text_into_slide.replace("\n\n", "\n").split('\n')  # Split the text into individual points

    # Find the content placeholder in the slide
    content_placeholder = None
    for placeholder in found_slide.placeholders:
        if placeholder.placeholder_format.idx == 1:  
            content_placeholder = placeholder
            break

    if content_placeholder:
        text_frame = content_placeholder.text_frame

        # Split the text into chunks of 5 points each
        chunk_size = 5
        chunks = [text_into_slide_text[i:i + chunk_size] for i in range(0, len(text_into_slide_text), chunk_size)]

        for i, chunk in enumerate(chunks):
            if i > 0:
                # Create a new slide using the same layout
                new_slide = presentation.slides.add_slide(found_slide.slide_layout)

                # Find the content placeholder in the new slide
                new_content_placeholder = None
                for placeholder in new_slide.placeholders:
                    if placeholder.placeholder_format.idx == 1:
                        new_content_placeholder = placeholder
                        break

            # Use the content placeholder from the first slide for the first chunk
            current_content_placeholder = content_placeholder if i == 0 else new_content_placeholder

            text_frame = current_content_placeholder.text_frame

            # Set the font size and style for the new text
            for point in chunk:
                p = text_frame.add_paragraph()
                run = p.add_run()
                run.text = point
                font = run.font
                font.size = Pt(9)  # Set font size to 9pt
                font.name = "Arial"  # Set font to Arial

        # Reorder the slides to place the new slide immediately after the found slide
        presentation.slides._sldIdLst.insert(found_slide_index + 1, new_slide._element)

        # Save the modified presentation
        presentation.save("23102023_Presentation.pptx")
        print("Text added to the 'Advantages' slide and subsequent slides.")
    else:
        print("Content placeholder not found on the 'Advantages' slide.")
else:
    print("Slide not found.")

修改后的代码

from pptx import Presentation
from pptx.util import Pt
from pptx.enum.text import PP_ALIGN

# Load the PowerPoint presentation
pptx_file = "23102023_Presentation.pptx"
presentation = Presentation(pptx_file)

# Search for the slide titled "Advantages"
target_slide_title = "Advantages"
found_slide = None
found_slide_index = -1

for i, slide in enumerate(presentation.slides):
    if slide.shapes.title.text == target_slide_title:
        found_slide = slide
        found_slide_index = i
        break

# Check if the slide was found
if found_slide is not None:
    # Define the text to insert
    text_into_slide_text = text_into_slide.replace("\n\n", "\n").split('\n')  # Split the text into individual points

    # Find the content placeholder in the slide
    content_placeholder = None
    for placeholder in found_slide.placeholders:
        if placeholder.placeholder_format.idx == 1:  
            content_placeholder = placeholder
            break

    if content_placeholder:
        # Split the text into chunks of 5 points each
        chunk_size = 5
        chunks = [text_into_slide_text[i:i + chunk_size] for i in range(0, len(text_into_slide_text), chunk_size)]

        # 处理第一个chunk(填充原Advantages幻灯片)
        text_frame = content_placeholder.text_frame
        for point in chunks[0]:
            p = text_frame.add_paragraph()
            run = p.add_run()
            run.text = point
            font = run.font
            font.size = Pt(9)
            font.name = "Arial"

        # 处理剩余chunk,逐个插入到Advantages幻灯片之后
        for idx, chunk in enumerate(chunks[1:], start=1):
            # 创建新幻灯片
            new_slide = presentation.slides.add_slide(found_slide.slide_layout)
            # 将新幻灯片插入到指定位置,确保紧跟目标幻灯片序列
            presentation.slides._sldIdLst.insert(found_slide_index + idx, new_slide._element)

            # 找到新幻灯片的内容占位符
            new_content_placeholder = None
            for placeholder in new_slide.placeholders:
                if placeholder.placeholder_format.idx == 1:
                    new_content_placeholder = placeholder
                    break

            if new_content_placeholder:
                text_frame = new_content_placeholder.text_frame
                for point in chunk:
                    p = text_frame.add_paragraph()
                    run = p.add_run()
                    run.text = point
                    font = run.font
                    font.size = Pt(9)
                    font.name = "Arial"

        # 保存修改后的演示文稿
        presentation.save("23102023_Presentation.pptx")
        print("Text added to the 'Advantages' slide and subsequent slides.")
    else:
        print("Content placeholder not found on the 'Advantages' slide.")
else:
    print("Slide not found.")

修改说明

  1. 拆分处理逻辑:将原幻灯片的内容填充与新幻灯片的创建、插入分开处理,避免循环内的逻辑混乱。
  2. 动态插入位置:针对每个需要新建的幻灯片,计算插入位置为found_slide_index + idx(idx从1开始),确保每张新幻灯片都紧跟在Advantages幻灯片或前一张新建幻灯片之后,不会被添加到文档末尾。
  3. 修复原代码bug:原代码仅移动了最后一张新建幻灯片,现在所有新建幻灯片都会被正确插入到目标位置。

内容的提问来源于stack exchange,提问作者yildirimdata

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:13:13