我想知道是否有人知道如何在 Google App Engine for Python 中使用 xlsxwriter。该文档仅显示如何打开、写入和保存到文件。我已经研究过使用 StringIO 用于其他 Excel 库的解决方法,但它们似乎无法转移到 xlsxwriter。主要原因似乎是在其他库中您可以提供 StringIO 缓冲区,而在 xlsxwriter 中您只能提供文件名的字符串。
我有一个使用 pyexcelerator 的基本解决方案,但 xlsxwriter 的功能更加丰富,如果可能的话,我想使用它。
更新:issue已由 xlsxwriter 作者修复(自 0.4.8 版本起有效)。请参阅example.
依赖于我的回答这个线程,这是应该在 GAE 上起作用的:
from xlsxwriter.workbook import Workbook
class IndexHandler(webapp2.RequestHandler):
def get(self):
book = Workbook(self.response.out)
sheet = book.add_worksheet('test')
sheet.write(0, 0, 'Hello, world!')
book.close()
# construct response
self.response.headers['Content-Type'] = 'application/ms-excel'
self.response.headers['Content-Transfer-Encoding'] = 'Binary'
self.response.headers['Content-disposition'] = 'attachment; filename="workbook.xls"'
但是,它会抛出一个错误:
NotImplementedError:只有 tempfile.TemporaryFile 可供使用
because xlsxwriter
尝试使用写入临时目录tempfile.tempdir
不管怎样,看看source of _store_workbook
方法。而且,GAE 不允许tempfile
项目中要使用的模块:参见source,因为,如您所知,无法访问那里的磁盘。
于是,这里就形成了“恶性循环”。也许你应该考虑修改_store_workbook
使其完全在内存中工作的方法。或者,也许你可以mock
tempfile.tempdir
即时调用并将其替换为您自己的内存中对象。
另一种选择是创建一个问题xlsxwriter
问题跟踪器,我打赌@jmcnamara 在这个主题上有一些好主意。
希望有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)