首页 > 解决方案 > 使用带有现有模板的 powerpoint 文件

问题描述

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

标签: pythonpowerpointpython-pptx

解决方案


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

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)

推荐阅读