将Powerpoint文件与现有模板一起使用

时间:2018-07-13 13:40:02

标签: python powerpoint python-pptx

我正在使用python-pptx生成PowerPoint文件。如何使文件维护文件上的现有模板,而不会覆盖文件上的模板。

1 个答案:

答案 0 :(得分:1)

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

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)