Пример #1
0
    def __init__(self,
                 range_string=None,
                 min_col=None,
                 min_row=None,
                 max_col=None,
                 max_row=None,
                 title=None):
        if range_string is not None:
            try:
                title, (min_col, min_row, max_col,
                        max_row) = range_to_tuple(range_string)
            except ValueError:
                min_col, min_row, max_col, max_row = range_boundaries(
                    range_string)

        self.min_col = min_col
        self.min_row = min_row
        self.max_col = max_col
        self.max_row = max_row
        self.title = title

        if min_col > max_col:
            fmt = "{max_col} must be greater than {min_col}"
            raise ValueError(fmt.format(min_col=min_col, max_col=max_col))
        if min_row > max_row:
            fmt = "{max_row} must be greater than {min_row}"
            raise ValueError(fmt.format(min_row=min_row, max_row=max_row))
Пример #2
0
    def _initialise_columns(self):
        """
        Create a list of table columns from a cell range
        Always set a ref if we have headers (the default)
        Column headings must be strings and must match cells in the worksheet.
        """

        min_col, min_row, max_col, max_row = range_boundaries(self.ref)
        for idx in range(min_col, max_col + 1):
            col = TableColumn(id=idx, name="Column{0}".format(idx))
            self.tableColumns.append(col)
        if self.headerRowCount:
            self.autoFilter = AutoFilter(ref=self.ref)
Пример #3
0
    def __getitem__(self, key):
        """Convenience access by Excel style coordinates

        The key can be a single cell coordinate 'A1', a range of cells 'A1:D25',
        individual rows or columns 'A', 4 or ranges of rows or columns 'A:D',
        4:10.

        Single cells will always be created if they do not exist.

        Returns either a single cell or a tuple of rows or columns.
        """
        if isinstance(key, slice):
            if not all([key.start, key.stop]):
                raise IndexError(
                    "{0} is not a valid coordinate or range".format(key))
            key = "{0}:{1}".format(key.start, key.stop)

        if isinstance(key, int):
            key = str(key)
        min_col, min_row, max_col, max_row = range_boundaries(key)

        if not any([min_col, min_row, max_col, max_row]):
            raise IndexError(
                "{0} is not a valid coordinate or range".format(key))

        if not min_row:
            cols = tuple(self.iter_cols(min_col, max_col))
            if min_col == max_col:
                cols = cols[0]
            return cols
        if not min_col:
            rows = tuple(
                self.iter_rows(min_col=min_col,
                               min_row=min_row,
                               max_col=self.max_column,
                               max_row=max_row))
            if min_row == max_row:
                rows = rows[0]
            return rows
        if ":" not in key:
            return self._get_cell(min_row, min_col)
        return tuple(
            self.iter_rows(min_row=min_row,
                           min_col=min_col,
                           max_row=max_row,
                           max_col=max_col))
Пример #4
0
 def boundaries(self):
     return range_boundaries(self.ref)
Пример #5
0
    def iter_rows(self,
                  range_string=None,
                  min_row=None,
                  max_row=None,
                  min_col=None,
                  max_col=None,
                  row_offset=0,
                  column_offset=0):
        """
        Produces cells from the worksheet, by row. Specify the iteration range
        using indices of rows and columns.

        If no indices are specified the range starts at A1.

        If no cells are in the worksheet an empty tuple will be returned.

        :param range_string: range string (e.g. 'A1:B2') *deprecated*
        :type range_string: string

        :param min_col: smallest column index (1-based index)
        :type min_col: int

        :param min_row: smallest row index (1-based index)
        :type min_row: int

        :param max_col: largest column index (1-based index)
        :type max_col: int

        :param max_row: smallest row index (1-based index)
        :type max_row: int

        :param row_offset: added to min_row and max_row (e.g. 4)
        :type row_offset: int

        :param column_offset: added to min_col and max_col (e.g. 3)
        :type column_offset: int

        :rtype: generator
        """

        if range_string is not None:
            warn(
                "Using a range string with iter_rows is deprecated. Use ws[range_string]"
            )
            min_col, min_row, max_col, max_row = range_boundaries(
                range_string.upper())

        if self._current_row == 0 and not any(
            [min_col, min_row, max_col, max_row]):
            return ()

        min_col = min_col or 1
        min_row = min_row or 1
        max_col = max_col or self.max_column
        max_row = max_row or self.max_row

        if max_col is not None:
            max_col += column_offset
        if max_row is not None:
            max_row += row_offset
        return self._cells_by_row(min_col + column_offset,
                                  min_row + row_offset, max_col, max_row)