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
605 stars 43 forks source link

Set formatting for specific cell #33

Closed Adamits closed 1 year ago

Adamits commented 3 years ago

Hi,

Thanks for the awesome tool. I see that there is a set_style method for setting the style for a specific column by index. Is there a way to do this for a specific cell (e.g. indexed by the [x, y] row/col pair)? What i'd like to do is set the highest value of each column to be bolded (this is common when making tables for results of experiments) - so if there is any support for doing exactly that, which I have missed, being directed to it would be greatly appreciated :).

It would be cool to pass a function that tells the writer to iterate over columns and apply it, like:

writer.iter_col_styles(max, Style(font_weight="bold"))

or something like that...

Thanks!

thombashi commented 3 years ago

@Adamits Hi,

You can apply styles to specific cells by writing a style filter function and add it to a table writer instance via add_style_filter method.

An example of style filter function is as follows: https://github.com/thombashi/pytablewriter-altrow-theme/blob/3531ecbc0936f7be7344f8bb7313c24ebf95da44/pytablewriter_altrow_theme/__init__.py#L42-L57