4.5. Specify Cell Styles For Each Column

Writers can specify Style for each column by column_styles attribute of writer classes.

Sample Code
import pytablewriter as ptw
from pytablewriter.style import Style


def main():
    writer = ptw.MarkdownTableWriter(
        table_name="set style by column_styles",
        headers=[
            "auto align",
            "left align",
            "center align",
            "bold",
            "italic",
            "bold italic ts",
        ],
        value_matrix=[
            [11, 11, 11, 11, 11, 11],
            [1234, 1234, 1234, 1234, 1234, 1234],
        ],
        column_styles=[
            Style(),
            Style(align="left"),
            Style(align="center"),
            Style(font_weight="bold"),
            Style(font_style="italic"),
            Style(font_weight="bold", font_style="italic", thousand_separator=","),
        ],  # specify styles for each column
    )
    writer.write_table()


if __name__ == "__main__":
    main()
Output
# set style by styles
|auto align|left align|center align|  bold  |italic|bold italic ts|
|---------:|----------|:----------:|-------:|-----:|-------------:|
|        11|11        |     11     |  **11**|  _11_|      _**11**_|
|      1234|1234      |    1234    |**1234**|_1234_|   _**1,234**_|

Rendering result

You can also set Style to a specific column with index or header by using set_style method:

Sample Code
from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Style

def main():
    writer = MarkdownTableWriter()
    writer.headers = ["A", "B", "C",]
    writer.value_matrix = [[11, 11, 11], [1234, 1234, 1234]]

    writer.table_name = "set style by column index"
    writer.set_style(1, Style(align="center", font_weight="bold"))
    writer.set_style(2, Style(thousand_separator=" "))
    writer.write_table()
    writer.write_null_line()

    writer.table_name = "set style by header"
    writer.set_style("B", Style(font_style="italic"))
    writer.write_table()

if __name__ == "__main__":
    main()
Output
# set style by column index
| A  |   B    |  C  |
|---:|:------:|----:|
|  11| **11** |   11|
|1234|**1234**|1 234|

# set style by header
| A  |  B   |  C  |
|---:|-----:|----:|
|  11|  _11_|   11|
|1234|_1234_|1 234|

4.6. Specify theme

The following command will install external predefined themes:

pip install pytablewriter[theme]

theme argument of writer constructor or set_theme method can set”” predefined style filters. altrow theme will colored rows alternatively:

Sample Code
import pytablewriter as ptw

writer = ptw.TableWriterFactory.create_from_format_name(
    "markdown",
    headers=["INT", "STR"],
    value_matrix=[[1, "hoge"], [2, "foo"], [3, "bar"]],
    margin=1,
    theme="altrow",
)
writer.write_table()
Output
https://github.com/thombashi/pytablewriter-altrow-theme/blob/master/ss/ptw-altrow-theme_example_default.png

Themes can be created as plugins like as follows: https://github.com/thombashi/pytablewriter-altrow-theme