tablespam.Excel package

Submodules

tablespam.Excel.xlsx_styles module

Styling options for tables exported to excel.

class tablespam.Excel.xlsx_styles.CellStyle(rows: list[int], cols: list[str], style: Callable[[Cell], None])

Bases: object

Cell styles are styles that are applied to specific cells in the data.

A cell style is defined by a list of row indexed, a list of column names, and a style. The style is a function that formats single cells of an openpyxl workbook.

Example

>>> from tablespam.Excel.xlsx_styles import CellStyle
>>> style = CellStyle(
...     rows=[1, 2],
...     cols=['column_1'],
...     style=lambda c: setattr(c, 'number_format', '0.00'),
... )
cols: list[str]
rows: list[int]
style: Callable[[Cell], None]
class tablespam.Excel.xlsx_styles.DataStyle(test: Callable[[DataFrame], bool], style: Callable[[Cell], None])

Bases: object

Data styles are styles that are applied to all columns of a specific type.

Each DataStyle is a combination of a test and a style.

The test is a function that is applied to the data column. It should check if the column is of a specific type and return either True or False.

The style is a function that is applied to a single cell in an openpyxl workbook and adds styling to that cell.

Example

>>> import polars as pl
>>> from tablespam.Excel.xlsx_styles import DataStyle
>>> # Define a test that checks if a single data column is of type double:
>>> def test_double(x: pl.DataFrame):
...     if len(x.columns) != 1:
...         raise ValueError('Multiple columns passed to test.')
...     return all([tp in [pl.Float32, pl.Float64] for tp in x.dtypes])
>>> style = DataStyle(
...     test=test_double, style=lambda c: setattr(c, 'number_format', '0.00')
... )
style: Callable[[Cell], None]
test: Callable[[DataFrame], bool]
class tablespam.Excel.xlsx_styles.XlsxStyles(bg_default: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_title: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_subtitle: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_header_lhs: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_header_rhs: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_rownames: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_data: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, bg_footnote: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function default_bg_style>, vline: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function vline_style>, hline: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function hline_style>, cell_title: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_title_style>, cell_subtitle: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_subtitle_style>, cell_header_lhs: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_header_lhs_style>, cell_header_rhs: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_header_rhs_style>, cell_rownames: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_rownames_style>, cell_data: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_data_style>, cell_footnote: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function cell_footnote_style>, merge_rownames: bool = True, merged_rownames_style: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function merged_rownames_style>, footnote_style: ~typing.Callable[[~openpyxl.cell.cell.Cell], None] = <function footnote_style>, data_styles: dict[str, ~tablespam.Excel.xlsx_styles.DataStyle] = <factory>, cell_styles: None | list[~tablespam.Excel.xlsx_styles.CellStyle] = None)

Bases: object

Defines styles for different elements of the table.

Each style element is a function that takes in a single cell of the openpyxl workbook and apply a style to that cell.

Parameters:
  • merge_rownames (bool) – Should adjacent rows with identical names be merged?

  • merged_rownames_style (Callable[[Cell], None]) – style applied to the merged rownames

  • footnote_style (Callable[[Cell], None]) – style applied to the table footnote

  • data_styles (Callable[[Cell], None]) – styles applied to the columns in the data set based on their classes (e.g., numeric, character, etc.). data_styles must be a dict of DataStyle. Note that styles will be applied in the order of the list, meaning that a later style may overwrite an earlier style.

  • cell_styles (list[CellStyle]) – an optional list with styles for selected cells in the data frame.

  • bg_default (Callable[[Cell], None]) – default color for the background of the table

  • bg_title (Callable[[Cell], None]) – background color for the title

  • bg_subtitle (Callable[[Cell], None]) – background color for the subtitle

  • bg_header_lhs (Callable[[Cell], None]) – background color for the left hand side of the table header

  • bg_header_rhs (Callable[[Cell], None]) – background color for the right hand side of the table header

  • bg_rownames (Callable[[Cell], None]) – background color for the row names

  • bg_data (Callable[[Cell], None]) – background color for the data

  • bg_footnote (Callable[[Cell], None]) – background color for the footnote

  • vline (Callable[[Cell], None]) – styling for all vertical lines added to the table

  • hline (Callable[[Cell], None]) – styling for all horizontal lines added to the table

  • cell_default (Callable[[Cell], None]) – default style added to cells in the table

  • cell_title (Callable[[Cell], None]) – style added to title cells in the table

  • cell_subtitle (Callable[[Cell], None]) – style added to subtitle cells in the table

  • cell_header_lhs (Callable[[Cell], None]) – style added to the left hand side of the header cells in the table

  • cell_header_rhs (Callable[[Cell], None]) – style added to the right hand side of the header cells in the table

  • cell_rownames (Callable[[Cell], None]) – style added to row name cells in the table

  • cell_data (Callable[[Cell], None]) – style added to data cells in the table

  • cell_footnote (Callable[[Cell], None]) – style added to footnote cells in the table

bg_data() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_default() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_footnote() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_header_lhs() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_header_rhs() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_rownames() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_subtitle() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

bg_title() None

Default background style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_data() None

Default style applied to data cells.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_footnote() None

Default style applied to footnote cells.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_header_lhs() None

Default style applied to left hand side of the table header.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_header_rhs() None

Default style applied to right hand side of the table header.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_rownames() None

Default style applied to rowname cells.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_styles: None | list[CellStyle] = None
cell_subtitle() None

Default subtitle style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

cell_title() None

Default title cell style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

data_styles: dict[str, DataStyle]
footnote_style() None

Default style applied to footnote.

Parameters:

cell (Cell) – Cell reference to which the style is applied

hline() None

Default horizontal line style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

merge_rownames: bool = True
merged_rownames_style() None

Default style applied to merged row name cells.

Parameters:

cell (Cell) – Cell reference to which the style is applied

vline() None

Default vertical line style.

Parameters:

cell (Cell) – Cell reference to which the style is applied

tablespam.Excel.xlsx_styles.default_data_styles() dict[str, DataStyle]

Defines the default styles that are applied to different data types.

Returns:

dict with default styles.

Return type:

dict[str, DataStyle]

tablespam.Excel.xlsx_styles.style_color(primary_color: str = 'ffffff') XlsxStyles

Provides a simple way to define a color scheme for tables.

By default, tables have a “light” theme, where the background is white and text / lines are black. Based on a primary color, style_color will create tables that use the primary color as background for all title, header, and row name cells and adapts the text color based on the primary color. The automatic adaption of the background color is implemented based on Mark Ransom and SudoPlz at <https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color>

Parameters:

primary_color (str, optional) – olor to be used for the title, header, and row names background. This must be a hex code for the color. Defaults to ‘ffffff’.

Returns:

Style object

Return type:

XlsxStyles

Module contents

Functions to translate TableSpam to Excel.