使用 pyExcelerator 与 Django 生成动态 Excel 文件。确保临时文件名唯一
我想根据 Django 的请求生成一个动态 Excel 文件。库 pyExcelerator 可以做到这一点,但我还没有找到任何方法来使用 Excel 文件的内容,而无需生成服务器端临时 Excel 文件、读取它、使用其内容并删除它。
问题是 pyExcelerator 提取 Excel 文件内容的唯一方法是通过以下方式保存它:
workbook = pyExcelerator.Workbook()
workbook.save("tmp_filename")
然后读取临时文件内容。我无法使用标准库“tempfile”,因为它不接受文件,只接受文件名。如何确保文件名是唯一的并且文件一旦使用就被删除?
I'd like to generate a dynamic Excel file on request from Django. The library pyExcelerator does this, but I haven't found any way to use the contents of the Excel file without generating a server-side temporary Excel file, reading it, using its contents and deleting it.
The problem is that pyExcelerator only way to extract the contents of the Excel file is saving it via:
workbook = pyExcelerator.Workbook()
workbook.save("tmp_filename")
And then read the temporary file contents. I can't use the standard library "tempfile" because it doesn't accept a file, just a filename. How can I ensure that the filename is unique and that the file is deleted once it has been used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pyExcelerator 未维护,但它有一个分支 xlwt,它得到维护并具有更多功能,包括允许您保存到任何类似文件的对象。这包括直接保存到 Django
HttpResponse
:pyExcelerator is unmaintained, but it has a fork, xlwt, which is maintained and has more features, including allowing you to save to any file-like object. This includes saving straight to a Django
HttpResponse
:为什么不能使用
tempfile
模块?怎么样:
Why can you not use the
tempfile
module?How about: