3
votes

I am using power point as the user interface of my web page to make a simple pictures. What I do today is create slides in ppt and then save each slide as a picture, then my web would download the picture and show it. I would like to automate this process as much as possible to get rid of this manual saving.

I have looked in the python-pptx package, but I couldn't find the function which gives me the possibility to save each slide as a picture. Could you help me?

1

1 Answers

0
votes

python-pptx is not the right tool for this task, as it does not interact with PowerPoint at all (it works without an installed version of PowerPoint). If you have PowerPoint installed on your system you could use e.g. comtypes module to control it from python. An example how to do it could be found in python-pptx-interface module:

 from comtypes.client import Constants, CreateObject

# ...

def save_pptx_as_png(png_foldername: str, pptx_filename: str, overwrite_folder: bool = False):
    if os.path.isdir(png_foldername) and not overwrite_folder:
        print(f"Folder {png_foldername} already exists. "
              f"Set overwrite_folder=True, if you want to overwrite folder content.")
        return

    powerpoint = CreateObject("Powerpoint.Application")
    pp_constants = Constants(powerpoint)

    pres = powerpoint.Presentations.Open(pptx_filename)
    pres.SaveAs(png_foldername, pp_constants.ppSaveAsPNG)
    pres.close()
    if powerpoint.Presentations.Count == 0:  # only close, when no other Presentations are open!
        powerpoint.quit()

This should give you an idea, how to write your own save_as_png function.

Or you use python-pptx-interface directly:

from pptx_tools import utils  # needs python-pptx-interface installed
# you should use full paths, to make sure PowerPoint can handle the paths
png_folder = ...
pptx_file = ... 

utils.save_pptx_as_png(png_folder, pptx_file)  # additional optional parameter overwrite_folder