2

I have to open multiple presentation using a loop, get some data and close it.

I cannot seem to find a way to close the ppt file in python pptx without saving/overwriting it.

I use the following to open the ppt file

pptfile = addressList[xyz]
prs = Presentation(pptfile)
slides = prs.slides

but I cannot find a way to close to presentation(prs) without saving in order to load the next ppt

I have a temporary work around which is as follows

prs.save(r"C:\Users\prashant.kumar\temp.pptx")

Since prs.save closes the ppt it works fine but I want a better way to close the ppt without creating a temp ppt file.

Is there an attribute of Presentation for python-pptx which I can use to close the ppt without saving?

Prashant Kumar
  • 501
  • 8
  • 26

3 Answers3

1

Is there an attribute of Presentation for python-pptx which I can use to close the ppt without saving?

Just... stop using it? pptx doesn't seem to have a __del__ so if you just stop using it without saving it should not save.

If that doesn't work for some reason (though that would be odd as, again, I don't see where it'd save the modified presentation), you can give pptx a file object, which you can close after having loaded the presentation:

pptfile = addressList[xyz]
with open(pptfile, 'rb') as f:
    prs = Presentation(f)
slides = prs.slides

it should not be able to modify the file then, even if it wants to.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • I can't stop using it. As I said above, I am using a loop where each iteration of loop will use a fresh `prs` for a new PPT file which is stored in `addressList[xyz]` So before I assign a new ppt file to `prs` I have to close/clear it before the loop iterates, else python-pptx throws an error – Prashant Kumar Mar 19 '20 at 12:42
  • What error does it throw? Because the documentation of pptx says nothing about closing Presentation objects and `__del__` is not used anywhere and a cursory trawling doesn't seem to show any modification (let alone significant) performed by `save`. – Masklinn Mar 19 '20 at 13:52
1

python-pptx does not hold a file open while you are working on it. When you call:

prs = Presentation(pptfile)

The file pptfile is opened, read into memory, and closed, all before returning the Presentation object. So you can just use the prs reference for whatever you need and then abandon it for garbage collection, the same as any other object in Python.

You don't mention in your question a problem with some operation not being permitted because a file is open, so I assume you just thought that would be a problem and haven't actually encountered one.

scanny
  • 26,423
  • 5
  • 54
  • 80
0

You could try this:

import os

os.system("TASKKILL /F /IM powerpnt.exe")
Bob Smith
  • 220
  • 4
  • 21