【问题标题】:Xlsxwriter Error in Google App EngineGoogle App Engine 中的 Xlsxwriter 错误
【发布时间】:2014-03-27 07:41:33
【问题描述】:

我正在尝试在 Google App Engine 中使用 xlsxwriter,但我不断收到错误消息:'Response' object has no attribute 'tell'。我看过以前的问题Using xlsxwriter in Google App Engine for Python & XlsxWriter object save as http response to create download in Django 但我仍然收到此错误。我试图修改他们在文档https://github.com/jmcnamara/XlsxWriter/blob/master/examples/http_server_py2.py

中给出的示例

我目前在 GAE 中使用 xlwt 没有任何问题。

    output = StringIO.StringIO()

    # Even though the final file will be in memory the module uses temp
    # files during assembly for efficiency. To avoid this on servers that
    # don't allow temp files, for example the Google APP Engine, set the
    # 'in_memory' constructor option to True:
    workbook = xlsxwriter.Workbook(self.response.out,{'in_memory': True})
    worksheet = workbook.add_worksheet()

    # Write some test data.
    worksheet.write(0, 0, 'Hello, world!')

    # Close the workbook before streaming the data.
    workbook.close()

    # Rewind the buffer.
    output.seek(0)

    # Construct a server response.

    self.response.headers['Content-disposition'] = 'attachment; filename=test.xls'
    self.response.headers['Content-Transfer-Encoding'] = 'Binary'
    self.response.headers['Content-Type'] =  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

【问题讨论】:

    标签: python google-app-engine xlsxwriter


    【解决方案1】:

    你有几个问题。

    您正在创建一个 StringIO ,但您没有使用它。你的xlsxwriter.Workbook( 应该使用output 而不是self.response.out

    其次你为什么要output.seek(0)

    关闭工作簿后,您应该从output 获取字符串并将其写入self.response

    最后,如果您遇到错误,在提出问题时将堆栈跟踪添加到问题中总是很有用的。这样我们就可以看到代码中的哪个语句实际上导致了引发异常。

    【讨论】:

    • 谢谢我通过插入 self.response.out.write(output.getvalue()) 让它工作了
    猜你喜欢
    • 2013-03-09
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多