Question

Firstly i need to know the value of the cell and if the value of the cell is more than 1 or 1 then it will print into the excel sheet. Right now its working.

Secondly what I want if the cell had more than 1 or 1 value it should print the column name together with the cell value. For example:

while curr_row < total_rows:
    curr_row+=1
    curr_cols=-1
    while curr_cols<total_cols:
            curr_cols+=1
            cell_type=sheet1.cell_type(curr_row,curr_cols)
            print sheet1.cell_value(curr_row,curr_cols)
            if cell_type==1 or (cell_type==2 and sheet1.cell_value(curr_row,curr_cols)>=1):
                    Sheet.write(curr_row,curr_cols,sheet1.cell_value(curr_row,curr_cols))
                    print 'Current:', curr_cols   



0   0   0   0   0   0   0   0   0   0   0   0

0   0   0   0   0   0   0   0   1   0   0   0

0   1   0   0   0   0   0   0   0   0   0   0   

0   0   0   0   0   0   0   1   3   0   3   3

21  18  1   1   0   2   1   0   0   1   9   0   
 0  0   0   0   0   0   0   0   0   0   2   3

If value in the column 4 is 1 or greater than 1. So it should print 4:1. Its means 4 column and value is 1.

Was it helpful?

Solution

Here's a simplified version of your code. Plus, it prints column index and column value:

import xlrd

book_input = xlrd.open_workbook('input.xls')
sheet_input = book_input.sheet_by_index(0)

for row in xrange(sheet_input.nrows):
    for col in xrange(sheet_input.ncols):
        cell_type = sheet_input.cell_type(row, col)
        cell_value = sheet_input.cell_value(row, col)
        if cell_type == 1 or (cell_type == 2 and cell_value >= 1):
            print "%s:%s" % (col + 1, cell_value)

Hope this is what you wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top