-2

I am working on generating a powerpoint file using python-pptx. How do I make the file maintain the existing template on the file without overwriting the template on the file.

K. Abhulimen
  • 137
  • 1
  • 13
  • So what was your question again? Man try to be more comprehensive – ThunderHorn Jul 13 '18 at 13:42
  • @KostadinSlavovI think I explained it well enough. powerpoint have templates that are used to create uniform slides. Whenever I use these templates they get overwritten so i was asking how do i prevent that. – K. Abhulimen Jul 13 '18 at 15:02
  • 1
    please see [ask] for tips on improving your questions in the future. It may not always be obvious what you are asking, and it is always helpful (to those who would help you) if you can show what you have already tried, and explain what is wrong, or errors, etc. – David Zemens Jul 13 '18 at 17:55

1 Answers1

1

Open the file, save to a new path, and then re-open from the new path:

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

Or, you could do this with 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)
David Zemens
  • 53,033
  • 11
  • 81
  • 130