如何通过pywin32自动确认保存幻灯片排练计时

Nicely 0 信誉分
2024-05-17T09:58:28.5766667+00:00

User's image

我的需求是通过自动排练计时来将pptx导出为mp4,但是我在使用pywin32自动排练计时的时候遇到了问题,代码如下

powerpoint = win32.Dispatch('PowerPoint.Application')
powerpoint.Visible = True
# powerpoint.ShowWindowsInTaskbar = True

presentation = powerpoint.Presentations.Open(r"C:\Users\Administrator\Desktop\ChatPPT_1691750176987.pptx")

slide_count = presentation.Slides.Count

presentation.SlideShowSettings.AdvanceMode = 3
presentation.SlideShowSettings.StartingSlide = 1
presentation.SlideShowSettings.EndingSlide = slide_count
presentation.SlideShowSettings.ShowWithNarration = False

slide_show = presentation.SlideShowSettings.Run()

view = slide_show.View

view.AcceleratorsEnabled = False


# 函数:手动翻页
def go_to_next_slide():
    view.Next()


def go_to_slide(index: int):
    view.GotoSlide(index)


for index in range(slide_count):
    time.sleep(0.1)  # 等待2秒
    # print(index + 1)
    go_to_slide(index + 1)

view.Exit()
print('over')

当代码执行到view.Exit()时,会弹出上图的框并阻塞后续操作,我该如何修改我的代码使其可以自动保留幻灯片计时

PowerPoint
PowerPoint
Microsoft 演示文稿图形产品系列,提供用于创建演示文稿和添加图形效果(如多媒体对象和带文本的特殊效果)的工具。
9 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. AlexZhu-MSFT 6,116 信誉分 Microsoft 供应商
    2024-05-21T00:55:58.15+00:00

    Hi,

    不知您是否尝试过修改 AdvanceMode 的值来控制幻灯片的行为?

    import win32com.client as win32
    import time
    
    powerpoint = win32.Dispatch('PowerPoint.Application')
    powerpoint.Visible = True
    # powerpoint.ShowWindowsInTaskbar = True
    
    presentation = powerpoint.Presentations.Open(r"path_to_your_slide.pptx")
    
    slide_count = presentation.Slides.Count
    
    presentation.SlideShowSettings.AdvanceMode = 1
    presentation.SlideShowSettings.StartingSlide = 1
    presentation.SlideShowSettings.EndingSlide = slide_count
    presentation.SlideShowSettings.ShowWithNarration = False
    
    slide_show = presentation.SlideShowSettings.Run()
    
    view = slide_show.View
    
    view.AcceleratorsEnabled = False
    
    
    # 函数:手动翻页
    def go_to_next_slide():
        view.Next()
    
    
    def go_to_slide(index: int):
        view.GotoSlide(index)
    
    
    for index in range(slide_count):
        time.sleep(0.5)  # 等待0.5秒
        # print(index + 1)
        go_to_slide(index + 1)
    
    view.Exit()
    presentation.close()
    print('over')
    

    Regards, Alex

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。