openpyxl.worksheet.cell_range module

class openpyxl.worksheet.cell_range.CellRange(range_string=None, min_col=None, min_row=None, max_col=None, max_row=None, title=None)[source]

Bases: Serialisable

Represents a range in a sheet: title and coordinates.

This object is used to perform operations on ranges, like:

  • shift, expand or shrink

  • union/intersection with another sheet range,

We can check whether a range is:

  • equal or not equal to another,

  • disjoint of another,

  • contained in another.

We can get:

  • the size of a range.

  • the range bounds (vertices)

  • the coordinates,

  • the string representation,

property bottom

A list of cell coordinates that comprise the bottom of the range

property bounds

Vertices of the range as a tuple

property cells
property cols

Return cell coordinates as columns

property coord

Excel-style representation of the range

expand(right=0, down=0, left=0, up=0)[source]

Expand the range by the dimensions provided.

Parameters:
  • right (int) – expand range to the right by this number of cells

  • down (int) – expand range down by this number of cells

  • left (int) – expand range to the left by this number of cells

  • up (int) – expand range up by this number of cells

intersection(other)[source]

Return a new range with cells common to this range and other

Parameters:

other (openpyxl.worksheet.cell_range.CellRange) – Other sheet range.

Returns:

the intersecting sheet range.

Raise:

ValueError if the other range doesn’t intersect with this range.

isdisjoint(other)[source]

Return True if this range has no cell in common with other. Ranges are disjoint if and only if their intersection is the empty range.

Parameters:

other (openpyxl.worksheet.cell_range.CellRange) – Other sheet range.

Returns:

True if the range has no cells in common with other.

issubset(other)[source]

Test whether every cell in this range is also in other.

Parameters:

other (openpyxl.worksheet.cell_range.CellRange) – Other sheet range

Returns:

True if range <= other.

issuperset(other)[source]

Test whether every cell in other is in this range.

Parameters:

other (openpyxl.worksheet.cell_range.CellRange) – Other sheet range

Returns:

True if range >= other (or other in range).

property left

A list of cell coordinates that comprise the left-side of the range

max_col

Values must be of type <class ‘int’>

max_row

Values must be of type <class ‘int’>

min_col

Values must be of type <class ‘int’>

min_row

Values must be of type <class ‘int’>

property right

A list of cell coordinates that comprise the right-side of the range

property rows

Return cell coordinates as rows

shift(col_shift=0, row_shift=0)[source]

Shift the focus of the range according to the shift values (col_shift, row_shift).

Parameters:
  • col_shift (int) – number of columns to be moved by, can be negative

  • row_shift (int) – number of rows to be moved by, can be negative

Raise:

ValueError if any row or column index < 1

shrink(right=0, bottom=0, left=0, top=0)[source]

Shrink the range by the dimensions provided.

Parameters:
  • right (int) – shrink range from the right by this number of cells

  • down (int) – shrink range from the top by this number of cells

  • left (int) – shrink range from the left by this number of cells

  • up (int) – shrink range from the bottom by this number of cells

property size

Return the size of the range as a dictionary of rows and columns.

property top

A list of cell coordinates that comprise the top of the range

union(other)[source]

Return the minimal superset of this range and other. This new range will contain all cells from this range, other, and any additional cells required to form a rectangular CellRange.

Parameters:

other (openpyxl.worksheet.cell_range.CellRange) – Other sheet range.

Returns:

a CellRange that is a superset of this and other.

class openpyxl.worksheet.cell_range.MultiCellRange(ranges={})[source]

Bases: Strict

add(coord)[source]

Add a cell coordinate or CellRange

ranges

Use a set to keep values unique

remove(coord)[source]
sorted()[source]

Return a sorted list of items