Skip to content
Advertisement

How to avoid zipfile error with python-pptx saving files

I am using the python-pptx package to create a number of .pptx files from a series of dataframes. All works well with adding slides and such until it comes time to call prs.save() where “prs” is the Presentation. Doing so leads to a zipfile error re: open handles needing to be closed. I have done some research on the history of this problem (with python 2.6) but can’t figure out why it’s happening here with Python 3.7

[Errno 95] Operation not supported
Exception ignored in: <function ZipFile.__del__ at 0x7f15f2814e18>
Traceback (most recent call last):
  File "/usr/lib/python3.7/zipfile.py", line 1789, in __del__
    self.close()
  File "/usr/lib/python3.7/zipfile.py", line 1798, in close
    raise ValueError("Can't close the ZIP file while there is "
ValueError: Can't close the ZIP file while there is an open writing handle on it. Close the writing handle before closing the zip.

I am running this on a Databricks cluster where I installed python-pptx from pypi, so my ability to alter underlying packages is a bit more limited / complicated than if I was doing this on my local machine.

Also, I have tried

with open("new_ppt.pptx", "w") as f:
    prs = Presentation(f)

but this gives an error about the file not being of type zip.

What would be a solid option for finding a way to avoid this error while still being able to create the PPTX files?

Advertisement

Answer

If it works locally (which I imagine it will) but not on the Databricks cluster, I would look there for the problem. Maybe it’s filesystem isn’t quite the same as a regular machine or something. I know some environments don’t allow unrestricted writing of files.

Another thing you can try is writing to a BytesIO object (“in-memory” file) and see if that works. I don’t know if that directly solve your problem or not, but it would be an interesting additional datapoint for reasoning about what’s happening.

from io import BytesIO

pptx_file = BytesIO()
prs.save(pptx_file)

# ---then maybe---
with open("deck.pptx", "wb") as f:
    f.write(pptx_file.getvalue())
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement