Typeerror at /export_to_xls/ unorderable types: str() < int()

Asked

Viewed 44 times

-1

views

def export_to_xls(request):
    obitos = Obito.objects.all()
    response = HttpResponse(content_type='application/vnd.openxmlformats-    officedocument.spreadsheetml.sheet',)
    response['Content-Disposition'] = 'attachment; filename={date}-movies.xlsx'.format(date=datetime.now().strftime('%Y-%m-%d'),)

    workbook = Workbook()
    worksheet = workbook.active
    worksheet.titulo = "teste-Obitos"

    columns = [
    'nome_obito',
    'idade_obito',
  ]
    row_num = 1

    for col_nome, col_idade in enumerate(columns, 1):
     cell = worksheet.cell(row=row_num, column=col_idade)
     cell.value = col_idade

    for obito in obitos:
     row_num += 1
     row = [
        obito.nome_obito,
        obito.col_idade
     ]
     for col_num, cell_value in enumerate(row, 1):
        cell = worksheet.cell(row=row_num, column=col_num)
        cell.value = cell_value
    workbook.save(response)
    return response

I have this problem in the generation of xls! Any suggestions? Thank you.

  • Hello @sedarky avoid placing images in your questions, put the error message in Code Sample in the "{ }" field so we can help you.

1 answer

1


Please edit your question:

  • Code indentation is wrong;
  • The mistake is not with the traceback in addition to part of the traceback is in image form, where it should be plain text;
  • The mistake is in the title, where it should be a question

REPLY:

col_idade is a str (comes from the list columns which has only strs) but you are passing it to the parameter columns= of the method cell(). This parameter should receive an integer number (column number) and not a name.

Try to pass the current position of the list:

for col_posicao, col_titulo in enumerate(columns, 1):
    cell = worksheet.cell(row=row_num, column=col_posicao)
    cell.value = col_titulo
  • Solved! It was that same nosklo! Thank you.

  • 1

    @sedarky is not solved yet, the question has been fixed and the answer marked as accepted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.