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

JavaScript

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

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement