Example #1
0
def cell_style(
    *,
    wb: Workbook,
    style_name: str = 'cell_style',
    font_name: Optional[str] = 'Lucida Sans',
    font_size: Optional[int] = 11,
    font_bold: Optional[bool] = True,
    font_colour: Optional[str] = '000000',
    horizontal_alignment: Optional[str] = 'center',
    vertical_alignment: Optional[str] = 'center',
    wrap_text: Union[str, bool] = None,
    fill_type: Union[str, bool] = 'solid',
    foreground_colour: Union[str, bool] = 'd9d9d9',
    border_style: Union[str, bool] = None,
    border_colour: Union[str, bool] = None,
    number_format: Union[str, bool] = None
) -> NamedStyle:
    """
    Define a cell style

    Parameters
    ----------
    wb : Workbook
        The workbook in which to define the cell style.
    style_name : str = 'cell_style'
        The name for the cell style.
    font_name : Optional[str] = 'Lucida Sans'
        The font name for the style.
    font_size : Optional[int] = 11
        The font size for the style.
    font_bold : Optional[bool] = True
        A boolean or string to apply bold style.
    font_colour : Optional[str] = 'ffffff'
        The string for the font colour.
    horizontal_alignment : Optional[str] = 'center'
        The string for horizontal alignment.
    vertical_alignment : Optional[str] = 'center'
        The string for vertical alignment.
    wrap_text : Union[str, bool] = None
        A boolean or string to wrap text.
    fill_type : Optional[str] = 'solid'
        The string for the fill type.
    foreground_colour : Optional[str] = 'd9d9d9'
        The string for the foreground colour.
    border_style : Union[str, bool] = None
        A boolean or string to apply a border.
    border_colour : Union[str, bool] = None
        A boolean or string to apply a border colour.
    number_format : Union[str, bool] = None
        A boolean or string to apply a number format.

    Returns
    -------
    row_style : NamedStyle
        The named style.

    Example
    -------
    >>> red_cell_style = ds.cell_style(
    >>>     style_name='red_cell_style',
    >>>     font_colour='ffffff',
    >>>     foreground_colour='c00000'
    >>> )
    >>> wb.add_named_style(red_cell_style)
    >>> for cell in ['C1', 'D1', 'E1']:
    >>>     ws[cell].style = red_cell_style
    """
    cell_style = NamedStyle(name=style_name)
    cell_style.font = Font(
        name=font_name,
        size=font_size,
        bold=font_bold,
        color=font_colour
    )
    cell_style.alignment = Alignment(
        horizontal=horizontal_alignment,
        vertical=vertical_alignment,
        wrap_text=wrap_text
    )
    cell_style.fill = PatternFill(
        fill_type=fill_type,
        fgColor=foreground_colour
    )
    cell_style.border = Border(
        bottom=Side(
            border_style=border_style,
            color=border_colour
        )
    )
    cell_style.number_format = number_format
    wb.add_named_style(cell_style)
    return (wb, cell_style)