How to serve Excel files on Google App Engine

Google App Engine is a hosting service for your Python and, lately, Java applications. In this case I’m going to talk about Python. Since an application cannot save files it might seem hard to serve an Excel file. Thankfully a great library pyExcelerator is very flexibile and instead of saving a file you can offer a user to download the dynamically generated Excel file. Here is what you do:

from pyExcelerator import *
 
class MainPage(webapp.RequestHandler):
    def get(self):
        wb = Workbook()
        ws0 = wb.add_sheet('Sheet 1')
        # Rows and columns count starts from 0
        for x in range(10):
            for y in range(10):
                # writing to a specific x,y
                ws0.write(x, y, "this is cell %s, %s" % (x,y))
 
        # HTTP headers to force file download
        self.response.headers['Content-Type'] = 'application/ms-excel'
        self.response.headers['Content-Transfer-Encoding'] = 'Binary'
        self.response.headers['Content-disposition'] = 'attachment; filename="workbook.xls"'
 
        # output to user
        wb.save(self.response.out)

Happy coding!