6

i have exported my list using xlwt :

response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=Countries.xls'

wb = xlwt.Workbook()
ws1 = wb.add_sheet('Countries')

ws1.write(0, 0, 'Country Name')
ws1.write(0, 1, 'Country ID') 
countries = Country.objects.all()
index = 1
for country in countries:
   ws1.write(index, 0, country.country_name)    
   ws1.write(index, 1, country.country_id)
   index +=1

It generate an Excel file with list of countries but the problem is the columns of the sheet are not adjusted to data generated. is there any solution to adjust those columns ?

Drwhite
  • 1,545
  • 4
  • 21
  • 44

1 Answers1

9

There's no built-in way to adjust column width to the data inside using xlwt.

The question was already asked here, so I'll just refer you:

Basically, you should just keep track of the maximum data length in each column and adjust the column width manually based on the font size and style.

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 3
    The proper action here is to vote to close (as duplicate), but since you don't have the rep yet, you've done the next best, which is to provide a nice summary, with good links to further reading. Worth an upvote! (Even though I did vote to close.) – John Y May 14 '13 at 15:35