Tuesday, April 23, 2013

Exporting data to Excel or PDF file in Django

Exporting data to Excel or PDF file is one of the most common tasks in web development, especially when you have to make reports. It's quite easy these day because of the open-source world. Here are some examples how to do it:

1. To Excel file:

I will use xlwt lib, a super easy-to-manipulate python lib. I know many people will consider the build-in csv module of python instead, but after a while I found that xlwt is so convenient, so I choose it.


Your views.py:


from django.http import HttpResponse
import xlwt
def myview(request):
     book = xlwt.Workbook(encoding='utf8')
     sheet = book.add_sheet('my_sheet')  
   
               # Adding style for cell
               # Create Alignment
               alignment = xlwt.Alignment()
             
               # horz May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT,    
               # HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL,
               # HORZ_DISTRIBUTED
       alignment.horz = xlwt.Alignment.HORZ_LEFT
               # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED,
               # VERT_DISTRIBUTED
       alignment.vert = xlwt.Alignment.VERT_TOP
       style = xlwt.XFStyle() # Create Style
       style.alignment = alignment # Add Alignment to Style

    # write the header
     header = ['Header 1', 'Header 2', 'Header 3', 'Header 4']
     for hcol, hcol_data in enumerate(header): # [(0,'Header 1'), (1, 'Header 2'), (2,'Header 3'), (3,'Header 4')]
           sheet.write(0, hcol, hcol_data, style=xlwt.Style.default_style)
 
    # write your data, you can also get it from your model
     data = ['genius', 'super', 'gorgeous', 'awesomeness']
     for row, row_data in enumerate(data, start=1): # start from row no.1
           for col, col_data in enumerate(row_data):
                 sheet.write(row, col, col_data, style=xlwt.Style.default_style)
      response = HttpResponse(mimetype='application/vnd.ms-excel')
      response['Content-Disposition'] = 'attachment; filename=my_data.xls'
      book.save(response)
      return response



2. To PDF file:

Googling around and you will see the name ReportLab appears so many times. ReportLab is a huge project which aims to help python developers do advanced report tasks. The Django Project's documentation also mention it as a method to work with pdf file in Django: https://docs.djangoproject.com/en/dev/howto/outputting-pdf/
But, ReportLab looks a little bit complicated to me, so I tried to search on github and found a wrapper of it, PDFDocument module: https://github.com/matthiask/pdfdocument which makes the job much easier.

Here is an example, in views.py:

from django.http import HttpResponse
from pdfdocument.document import PDFDocument
def my_view(request):
      response = HttpResponse(mimetype='application/pdf')
      response['Content-Disposition'] = 'attachment; filename=my_data.pdf'
      pdf = PDFDocument(response)
      pdf.init_report()
      pdf.h1('This is my data')
      pdf.p('It is so geniussssssss!!!!!')
      # You can use pdf.table('data') to create table
      # take a look at the source code and you will know how
      # https://github.com/matthiask/pdfdocument/blob/master/pdfdocument/document.py
      pdf.generate()
 
      return response



Awesome!