2

Im having troubles getting all the row of a column. i have this

def excel(request):
    file_location = "proyectos.xls"
    workbook = xlrd.open_workbook(file_location)
    sheet = workbook.sheet_by_index(0)
    for col in range(sheet.ncols):
        data = sheet.cell_value(0,col)
        return HttpResponse (data)

but this is returning the number of the all columns, does not return the values of the columns, how can i get all the values of a column excepting the header?

dfrojas
  • 673
  • 1
  • 13
  • 32
  • Currently that function isn't returning *anything*. Can you be more specific about what you want the function to do? – mechanical_meat Oct 28 '14 at 18:36
  • Sorry, i already edit the comment, it has to return the values of the column to 0 until "col" – dfrojas Oct 28 '14 at 18:41
  • With the code that you've presented, your `for` loop with only iterate once. – Celeo Oct 28 '14 at 18:41
  • Ok, thanks.How can i iterate all the cols? – dfrojas Oct 28 '14 at 18:43
  • 2
    Values of the columns are provided by `col_values()`. Please see the [tutorial](http://www.simplistix.co.uk/presentations/python-excel.pdf) (which is effectively the manual). – John Y Oct 28 '14 at 19:49
  • @JohnY Perhaps you mean `row_values()` as it the column value that is changing, not the row. That is, get the column values for a given row. – Dunes Oct 28 '14 at 21:08
  • @Dunes: No, I mean `col_values()`. To me, the question "how can i get all the values of a column excepting the header?" sounds tailor-made for `col_values()`, with the appropriate parameters. – John Y Oct 28 '14 at 21:31
  • @JohnY rereading the text I think you are correct. My assessment was based on the code snippet. – Dunes Oct 28 '14 at 22:20

1 Answers1

2

You could store all the cell values in a list, e.g.:

    ... 
    row = []
    for col in range(sheet.ncols):
        row.append( sheet.cell_value(0,col) )
    return render_to_response('your_template.html', {'row': row})

And in your template something like:

{% for i in row %}
    <p>{{ i }}</p>
{% endfor %}
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223