在Google App Engine for Python中使用xlsxwriter
作者:互联网
我想知道是否有人知道如何在Google App Engine for Python中使用xlsxwriter.该文档仅显示如何打开,写入和保存到文件.我已经查看了使用StringIO处理其他Excel库的变通方法,但它们似乎不能转换为xlsxwriter.主要原因似乎是在其他库中您可以提供StringIO缓冲区,而在xlsxwriter中,您只能提供文件名的字符串.
我有一个使用pyexcelerator的基本解决方案,但xlsxwriter功能更丰富,我想使用它,如果可能的话.
解决方法:
UPD:issue由xlsxwriter作者修复(自0.4.8版本起作用).见example.
依靠我在this thread的回答,这里有什么应该适用于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: Only tempfile.TemporaryFile is available for use
因为xlsxwriter无论如何都试图使用tempfile.tempdir写入临时目录,请参见_store_workbook方法的source.并且,GAE不允许在项目中使用tempfile模块:请参阅source,因为,如您所知,那里没有访问磁盘.
所以,这里是一个“恶性循环”.可能你应该考虑修改_store_workbook方法,使其完全在内存中工作.或者,您可以动态模拟tempfile.tempdir调用并将其替换为您自己的内存中对象.
另一种选择是在xlsxwriter issue tracker上创建一个问题,我打赌@jmcnamara在这个问题上有一些好主意.
希望有所帮助.
标签:python,google-app-engine,xlsxwriter 来源: https://codeday.me/bug/20191007/1864164.html