Question

I am trying to use xlsxwriter in Google App Engine but I keep on getting the Error: 'Response' object has no attribute 'tell'. I have looked at previous questions Using xlsxwriter in Google App Engine for Python & XlsxWriter object save as http response to create download in Django but I am still getting this error. I have tried to amend the example they give in the docs https://github.com/jmcnamara/XlsxWriter/blob/master/examples/http_server_py2.py

I am currently using xlwt in GAE without any issues.

    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'
Was it helpful?

Solution

You have a couple of problems.

You are creating a StringIO , but you are not using it. Your xlsxwriter.Workbook( should be using output not self.response.out

Secondly why are you doing output.seek(0)

After closing with workbook, you should be getting the string from output and writing that out to self.response

And lastly if you are getting an error, it's always useful when asking a question to add the stacktrace to the question. That way we can see which statement in your code is actually causing the exception to be raised.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top