Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xlwt和调整宽度使用python导出excel_Python_Excel_Xlwt - Fatal编程技术网

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

使用xlwt和调整宽度使用python导出excel,python,excel,xlwt,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, 'Co

我已使用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文件,但问题是工作表的列没有根据生成的数据进行调整。
有没有调整这些列的解决方案?

没有内置的方法可以使用
xlwt
将列宽调整到数据中

这里已经有人问了这个问题,所以我只向您介绍:

  • John Machin对该主题的回答
基本上,您应该只跟踪每列中的最大数据长度,并根据字体大小和样式手动调整列宽


希望这能有所帮助。

相关信息:正确的做法是投票关闭(作为副本),但由于您还没有代表,您已经做了下一个最好的选择,即提供一个好的摘要,并提供好的链接以供进一步阅读。值得投一票!(尽管我投了赞成票。)