def range_boundaries(range_string): """ Convert a range string into a tuple of boundaries: (min_col, min_row, max_col, max_row) Cell coordinates will be converted into a range with the cell at both end """ m = ABSOLUTE_RE.match(range_string) min_col, min_row, sep, max_col, max_row = m.groups() min_col = column_index_from_string(min_col) min_row = int(min_row) if max_col is None or max_row is None: max_col = min_col max_row = min_row else: max_col = column_index_from_string(max_col) max_row = int(max_row) return min_col, min_row, max_col, max_row
def read_dimension(source): min_row = min_col = max_row = max_col = None DIMENSION_TAG = '{%s}dimension' % SHEET_MAIN_NS DATA_TAG = '{%s}sheetData' % SHEET_MAIN_NS it = iterparse(source, tag=[DIMENSION_TAG, DATA_TAG]) for _event, element in it: if element.tag == DIMENSION_TAG: dim = element.get("ref") m = ABSOLUTE_RE.match(dim.upper()) min_col, min_row, sep, max_col, max_row = m.groups() min_row = int(min_row) if max_col is None or max_row is None: max_col = min_col max_row = min_row else: max_row = int(max_row) return min_col, min_row, max_col, max_row elif element.tag == DATA_TAG: # Dimensions missing break element.clear()
def range(self, range_string, row=0, column=0): """Returns a 2D array of cells, with optional row and column offsets. :param range_string: cell range string or `named range` name :type range_string: string :param row: number of rows to offset :type row: int :param column: number of columns to offset :type column: int :rtype: tuples of tuples of :class:`openpyxl.cell.Cell` """ _rs = range_string.upper() m = ABSOLUTE_RE.match(_rs) # R1C1 range if m is not None: rows = self.iter_rows(_rs, row_offset=row, column_offset=column) return tuple(row for row in rows) else: return self.get_named_range(range_string)