I am generating excel in my Django views.py but since column names are a bit long, I am having a hard time to set auto column fit width manually every time I/user downloads the excel.
Below is my working code snippet for excel generation using xlswriter.
def excel(request):
ans = request.POST.getlist('ans[]')
ans_final=[]
rows = request.POST.get('rows')
for each_ele in ans:
each_ele = each_ele.split('.')
each_ele[0] = each_ele[0][:-2]
each_ele[1] = each_ele[1][:-2]
fin = each_ele[0]+' - '+each_ele[1]
ans_final.append(fin)
workbook = xlsxwriter.Workbook('/home/Desktop/status.xlsx')
worksheet = workbook.add_worksheet('Test_Data')
bold = workbook.add_format({'bold': True})
for i in range(len(ans_final)):
worksheet.write(0, i,ans_final[i],bold)
row_index=1
row_count = int(rows)
while(row_count):
col_index=0
for each_ele in ans:
worksheet.write(row_index, col_index, eval(each_ele))
col_index += 1
row_index += 1
row_count -= 1
workbook.close()
return JsonResponse({'ok':'ok'})
Please suggest a work around for setting Auto-fit column width in the above code.