且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-18 17:59:34

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

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.

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

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