Search code examples
pythonexcelrangexlrd

Python XLRD use Range


I want to assign an excel range to a variable:

import xlrd

file = r"C:\Users\Lisa\Desktop\Frank\export.XLSX"
book = xlrd.open_workbook(file)
sheet = book.sheet_by_index(0)
data = [range("A3:D7")]

, but I get an error:

    data = [range("A3:D7")]
TypeError: 'str' object cannot be interpreted as an integer

Any ideas?


Solution

  • You could use the following to extract a block from an XLS file as follows:

    from itertools import product
    import xlrd
    
    workbook = xlrd.open_workbook(r"input.xls")
    sheet = workbook.sheet_by_index(0)
    
    # Build a cell lookup table
    cell_refs = {xlrd.cellname(r, c) : (c, r) for r, c in product(range(sheet.nrows), range(sheet.ncols))}
    
    def get_cell_range(start_col, start_row, end_col, end_row):
        return [sheet.row_slice(row, start_colx=start_col, end_colx=end_col+1) for row in range(start_row, end_row+1)]
    
    def get_cells(excel_range):
        start_range, end_range = excel_range.split(":")
        return get_cell_range(*cell_refs[start_range], *cell_refs[end_range])
    
    
    # Use numeric cell references
    data = get_cell_range(0, 2, 3, 6)   # A3 to D7
    print(data)
    
    # Use Excel range format
    data = get_cells("A3:D7")
    print(data)
    

    xlrd works using column and row numbers starting from 0 instead of Excel cell names. It can only convert Excel cell references from col row format to A1 format using xlrd.cellname(), not the other way around. A workaround is to build your own reverse lookup table using that function.

    Note: older versions of XLRD supported Excel XLSX files. Now only XLS files are supported. For XLSX files consider using openpyxl or Pandas.