Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the presentation without saving it in Python pptx

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?

like image 366
Prashant Kumar Avatar asked Dec 19 '25 07:12

Prashant Kumar


1 Answers

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.

like image 131
scanny Avatar answered Dec 20 '25 19:12

scanny