I am looking for a better (more readable / less hacked together) way of reading a range of cells using
from openpyxl import load_workbook
import numpy as np
firstCol = "B"
firstRow = 2
nCols = 2
nRows = 2
lastCol = chr(ord(firstCol) + nCols - 1)
cellRange = firstCol + str(firstRow) + ":" + lastCol + str(firstRow + nRows - 1)
wsName = "Sheet1"
wb = load_workbook(filename="worksheet.xlsx", data_only=True)
data = np.array([[i.value for i in j] for j in wb[wsName][cellRange]])
openpyxl provides functions for converting between numerical column indices (1-based index) and Excel's 'AA' style. See the
utils module for details.
However, you'll have little need for them in general. You can use the
get_squared_range() method of worksheets for programmatic access. And, starting with openpyxl 2.4, you can do the same with the
iter_cols() methods. NB.
iter_cols() is not available in read-only mode.