Excel export using python using xlwt and width adjustment

I 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 generates an Excel file with a list of countries, but the problem is that the columns of the worksheet are not configured for the generated data. is there any solution to customize these columns?

+4
source share
1 answer

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

A question has already been asked here, so I'm just sending you:

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

Hope this helps.

+5
source

Source: https://habr.com/ru/post/1480725/


All Articles