通过PowerPoint和Win32COM保存演示文稿太慢。

huangapple go评论59阅读模式
英文:

Saving a presentation through PowerPoint and Win32COM takes too long

问题

我想将多个 PowerPoint 文件中的幻灯片合并到单个输出文件中。

我有一个包含多个 PowerPoint 文件的文件夹,每个文件只包含一个幻灯片。我的现有代码创建一个新演示文稿,将输入文件中的幻灯片复制并粘贴到输出文件中。

我首先尝试了使用 python-pptx,但是关于这个主题的几篇 StackOverflow 帖子表明这几乎不可能,特别是因为我需要保持布局、图像、字体等的完整性。

在另一个 SO 帖子的帮助下(很遗憾我不知道来源),我编写了解决这个问题的函数:

def __mergePresentations(inputFileNames, outputFileName):
    Application = win32com.client.Dispatch("PowerPoint.Application")
    outputPresentation = Application.Presentations.Add()
    outputPresentation.SaveAs(outputFileName) # Save presentation to allow better modification

    print("Filling presentation...")
    for file in inputFileNames:
        print(f"- Copying {str(file)}")
        currentPresentation = Application.Presentations.Open(file)
        currentPresentation.Slides.Range(range(1, 2)).copy() # Only copy the first slide
        Application.Presentations(outputFileName).Windows(1).Activate()
        outputPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
        currentPresentation.Close()

    print("Saving presentation...")
    outputPresentation.Save()
    print("Closing presentation...")
    outputPresentation.Close()
    Application.Quit()
    print("Presentation has been successfully saved.")
    return

问题

这段代码可以运行,但出于某种原因,该程序有时需要超过 30 秒才能保存/退出。我知道 Win32com 可能会很慢,但这使得该程序在指定的环境中几乎无法使用。

我正在使用 Office 365 上的 PowerPoint,程序在 Python 3.9 上运行。代码立即打印出“演示文稿已成功保存”,但需要很长时间才能返回。

我是否忘记了正确保存文件的任何参数?这可能与我的环境有关吗?

谢谢!

英文:

I want to merge slides from multiple PowerPoint files into a single output file.

I have a folder filled with PowerPoint files that each only contain one slide. My current code creates a new presentation, copies the slides from the input files and pastes them into the output file.

I first tried this with python-pptx, but the few StackOverflow posts on this topic suggest that this is not or pretty much impossible, especially because I need to keep the layout, images, fonts, ... intact.

With some help from another SO post (I unfortunately do not know the source), I wrote a function that solves this problem:

def __mergePresentations(inputFileNames, outputFileName):
    Application = win32com.client.Dispatch("PowerPoint.Application")
    outputPresentation = Application.Presentations.Add()
    outputPresentation.SaveAs(outputFileName) # Save presentation to allow better modification

    print("Filling presentation...")
    for file in inputFileNames:
        print(f"- Copying {str(file)}")
        currentPresentation = Application.Presentations.Open(file)
        currentPresentation.Slides.Range(range(1, 2)).copy() # Only copy the first slide
        Application.Presentations(outputFileName).Windows(1).Activate()
        outputPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
        currentPresentation.Close()

    print("Saving presentation...")
    outputPresentation.Save()
    print("Closing presentation...")
    outputPresentation.Close()
    Application.Quit()
    print("Presentation has been successfully saved.")
    return

Problem:

This code works, but for some reason, the program sometimes takes more than 30 seconds to save/quit. I know that Win32com can be pretty slow, but this makes the program almost unusable in its designated environment.

I am using PowerPoint on Office 365 and the program runs on Python 3.9. The code prints "Presentation has been successfully saved" instantly, but takes forever to return.

Did I forget any arguments to save the file properly? Can this be related to my environment?

Thanks!

答案1

得分: 0

对于其他遇到此问题的人:跳过 Application.Quit(),而是通过调用 os.system('taskkill /F /IM POWERPNT.EXE') 来退出。

这会立即发生,并且在功能上与 Application.Quit() 相同(据我测试所示)。

英文:

For anyone else having this issue: Skip Application.Quit() and rather quit by calling os.system('taskkill /F /IM POWERPNT.EXE').

This happens immediately and has the same functionality as Application.Quit() (as far as I could tell from testing).

huangapple
  • 本文由 发表于 2023年5月22日 19:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定