Grid Cell

A Cell object stores the content of a Grid cell.

A cell can have styles, a dictionary of key-value properties attached to the cell.

A cell has a nature to distinguish between header, body and footer cells. The default nature is None, but you can also use “body”, “header”, “footer” or whatever…

A cell has top-left coordinates: x and y. The default coordinates is (1, 1): this is the top-left coordinate of the cell box. The coordinates x and y cannot be null: grid coordinates are 1-indexed.

A cell has a size: width and height. The default size is (1, 1), you can increase them to represent horizontal or vertical spanning. The width and the height cannot be null.

To instantiate a Cell, you can do:

>>> c1 = Cell("c1")
>>> c2 = Cell("c2", styles={'color': 'red'})
>>> c3 = Cell("c3", nature="footer")
>>> c4 = Cell("c4", width=2)
>>> c5 = Cell("c5", height=2)

The string representation of a cell is the string representation of it’s content:

>>> for cell in c1, c2, c3, c4, c5:
...     print(cell)

On initialization, the cell min position is always (1, 1), a.k.a. the top-left.

>>> c1 = Cell("c1")
>>> c1.min
Coord(x=1, y=1)
>>> c1.size
Size(width=1, height=1)
Box(min=Coord(x=1, y=1), max=Coord(x=1, y=1))

A cell can be moved to another position:

>>> c1 = Cell("c1", width=3, height=2)
>>> c2 = c1.move_to(Coord(5, 3))
>>> c2.min
Coord(x=5, y=3)
>>> c2.size
Size(width=3, height=2)
Box(min=Coord(x=5, y=3), max=Coord(x=7, y=4))

You can check if a coord is inside the box:

>>> c1 = Cell("c1", width=3, height=2)
>>> c2 = c1.move_to(Coord(5, 3))
>>> (7, 4) in c2
>>> Coord(6, 3) in c2
>>> Box(6, 3, 7, 4) in c2
class benker.cell.Cell(content, styles=None, nature=None, x=1, y=1, width=1, height=1)

Bases: benker.styled.Styled

Cell of a grid.


user-defined cell content. It can be of any type: None, str, int, float, a container (list), a XML element, etc. The same content can be shared by several cells, it’s your own responsibility to handle the copy (or deep copy) of the content reference when needed.


In a Grid, the merging of two cell contents is done with the “+” operator (__add__()). You can override this by using a content_appender, a two-arguments function which will perform the concatenation of the two contents.

Changed in version 0.4.2: The default value of nature is None (instead of “body”).


Bounding box of the cell.


Height of the cell – rows spanning.


Maximum coordinates of the cell – bottom-right coordinates.


Minimum coordinates of the cell – top-left coordinates.


Move the cell to the given coordinates.

See: transform().

Parameters:coord (tuple[int, int] or benker.coord.Coord) – new top-left coordinates of the cell, by default it is unchanged.
Return type:benker.cell.Cell
Returns:a copy of this cell after transformation.

Resize the cell to the given size.

See: transform().

Parameters:size (tuple[int, int] or benker.size.Size) – new size of the cell, by default it is unchanged.
Return type:benker.cell.Cell
Returns:a copy of this cell after transformation.

Size of the cell – (with, height).

transform(coord=None, size=None)

Transform the bounding box of the cell by making a move and a resize.

Return type:



a copy of this cell after transformation.


Width of the cell – columns spanning.


Try hard to extract a good string representation of the cell content.

>>> from benker.cell import get_content_text
>>> get_content_text(None) == ''
>>> get_content_text('') == ''
>>> print(get_content_text('Hi'))
>>> print(get_content_text(True))
>>> print(get_content_text(123))
>>> print(get_content_text(3.14))
>>> get_content_text([None, None]) == ''
>>> print(get_content_text(["hello", " ", "world!"]))
hello world!
Parameters:content – Cell content.
Returns:the cell text:
  • if the content is None: returns “”,
  • if the content is a string: return the string unchanged,
  • if the content is a number: return the string representation of the number,
  • if the content is a list of strings, return the concatenated strings (None is ignored),
  • if the content is a list of XML nodes, return the concatenated strings of the elements (the processing-instruction and the comments are ignored),
  • else: return a concatenation of the string representation of the content items.

New in version 0.4.1.

Changed in version 0.5.1: Returns the XML Element text and tail text.