使用xlwt和调整宽度用python导出excel

2024-05-19 19:47:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我已使用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文件,但问题是工作表中的列没有根据生成的数据进行调整。 有什么办法可以调整这些列吗?


Tags: 列表indexapplicationresponsecountrycountrieswritems
1条回答
网友
1楼 · 发布于 2024-05-19 19:47:48

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

这个问题已经在这里提过了,所以我只想告诉你:

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

希望能有所帮助。

相关问题 更多 >