Search code examples
pythonxlrd

Python/xrld Read rows and columns


I have exel file with specific structure. There are first row is title second are names and the last one are values. I need take only title and values it is not so difficult if Excel file has only 3 rows, but it can be 100 rows and columms and i need to get only Title and values, not any names and empty rows

         1          2        3           
1    GroupOne     Empty      Empty
2      Jonh       Elena     Mike    
3        45         100      500 
4        Empty      Empty     Empty
5   GroupTwo       Empty     Empty
6   Lisa           Ken       Lui
7   100             300      400

And etc.

As You see between two Title always is one Empty row. Finaly it will be something like this:

GroupOne
45   100   500
GroupTwo
100 300 400

Thank you in advance. I would be so grateful for some help


Solution

  • I think this website has an example that will help you:

    import xlrd
    workbook = xlrd.open_workbook('my_workbook.xls')
    worksheet = workbook.sheet_by_name('Sheet1')
    num_rows = worksheet.nrows - 1
    num_cells = worksheet.ncols - 1
    curr_row = -1
    while curr_row < num_rows:
      curr_row += 1
      row = worksheet.row(curr_row)
      print 'Row:', curr_row
      curr_cell = -1
      while curr_cell < num_cells:
        curr_cell += 1
        # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
        cell_type = worksheet.cell_type(curr_row, curr_cell)
        cell_value = worksheet.cell_value(curr_row, curr_cell)
        print ' ', cell_type, ':', cell_value
    

    The function cell_type should help you build a if-statement such as if worksheet.cell_type != 0 and thus skip empty cells.