如何避免python pptx保存文件时出现zipfile错误

如何避免python pptx保存文件时出现zipfile错误,python,python-3.7,databricks,zipfile,python-pptx,Python,Python 3.7,Databricks,Zipfile,Python Pptx,我正在使用python pptx包从一系列数据帧创建许多.pptx文件。在调用prs.save(prs是演示文稿的位置)之前,所有这些都可以很好地添加幻灯片等。这样做会导致zipfile错误:打开需要关闭的句柄。我已经用Python2.6研究了这个问题的历史,但不明白为什么在Python3.7中会出现这个问题 [Errno 95] Operation not supported Exception ignored in: <function ZipFile.__del__ at 0x7f1

我正在使用python pptx包从一系列数据帧创建许多.pptx文件。在调用prs.save(prs是演示文稿的位置)之前,所有这些都可以很好地添加幻灯片等。这样做会导致zipfile错误:打开需要关闭的句柄。我已经用Python2.6研究了这个问题的历史,但不明白为什么在Python3.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.
但这会导致文件不是zip类型的错误

在仍然能够创建PPTX文件的情况下,找到避免此错误的方法的可靠选项是什么


谢谢

如果它在本地工作,我想它会在Databricks集群上工作,但不会在Databricks集群上工作,我会在那里查找问题。也许它的文件系统与普通机器或其他东西不太一样。我知道有些环境不允许无限制地写入文件

您可以尝试的另一件事是写入内存文件中的BytesIO对象,看看是否有效。我不知道这是否能直接解决您的问题,但这将是一个有趣的额外数据点,用于推理正在发生的事情

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())

这很有效。PythonPPTX绝对是棒极了。非常感谢您的时间和努力!
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())