使用xlwt导出excel与python并调整宽度

时间:2013-05-14 13:45:09

标签: python excel xlwt

我使用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

它生成一个包含国家/地区列表的Excel文件,但问题是工作表的列未调整为生成的数据。 是否有任何解决方案来调整这些列?

1 个答案:

答案 0 :(得分:6)

没有内置的方法可以使用xlwt将列宽调整为内部数据。

这里已经提出了这个问题,所以我只想推荐你:

基本上,您应该只跟踪每列中的最大数据长度,并根据字体大小和样式手动调整列宽。

希望有所帮助。