thombashi / pytablewriter

pytablewriter is a Python library to write a table in various formats: AsciiDoc / CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
https://pytablewriter.rtfd.io/
MIT License
611 stars 43 forks source link

Header cells ignore style #37

Closed shawalli closed 1 year ago

shawalli commented 3 years ago

It appears that the header row discards the column_styles value. I also tried forcing a style via add_style_filter (see snippet below), but it was ignored. I ran into this on MarkdownTableWriter, but based on the code, I think it affects the base text writer (and subclassed writers) as well.


def force_header_style(cell, **kwargs):
    if cell.row == -1: 
        return None

    return Style(align="left", font_weight="bold")

writer = MarkdownTableWriter(
    headers=["a", "b", "c"],
    margin=1,
    value_matrix = [
        ["foo bar", "bar bax", "baz qux"],
    ].
)

writer.add_style_filter(force_header_style)

print(writer.dumps())

Expected Output:

| **a**   | **b**   | **c**   |
| :------ | :------ | :------ |
| foo bar | bar baz | baz qux |

Actual Output

|    a    |    b    |    c    |
| :------ | :------ | :------ |
| foo bar | bar baz | baz qux |
shawalli commented 3 years ago

I tried to dig in to figure out where this was happening in the code, but I got a bit confused when trying to trace the writing code

thombashi commented 3 years ago

Thank you for your feedback.

In the current implementation, no styles other than colors are applied to headers. In a future version, other styles might also be applicable to headers.

thombashi commented 1 year ago

pytablewriter 1.2.0 or later can apply styles to headers by style filters.