使用现有模板的 powerpoint 文件

Using a powerpoint file with existing template

我正在使用 python-pptx 生成 powerpoint 文件。如何使文件保持文件上现有的模板而不覆盖文件上的模板。

打开文件,保存到新路径,然后从新路径重新打开:

def create_new_presentation(templateFileName, outputFileName):
    """
        creates a new PPTX file from the template path -- this will OVERWRITE outputFileName if exists.
        templateFileName: path to 'template' file
        outputFileName: path to the output file
    """
    p = Presentation(templateFileName)
    p.save(outputFileName)
    p = Presentation(outputFileName)
    return p

或者,您可以使用 shutil.copyfile:

from shutil import copyfile

def create_new_presentation(templateFileName, outputFileName):
    """
        creates a new PPTX file from the template path -- this will OVERWRITE outputFileName if exists.
        templateFileName: path to 'template' file
        outputFileName: path to the output file
    """
    copyfile(templateFileName, outputFileName)
    return Presentation(outputFileName)