SkillAgentSearch skills...

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.

Install / Use

/learn @thombashi/Pytablewriter

README

.. contents:: pytablewriter :backlinks: top :depth: 2

Summary

pytablewriter <https://github.com/thombashi/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 / YAML.

.. image:: https://badge.fury.io/py/pytablewriter.svg :target: https://badge.fury.io/py/pytablewriter :alt: PyPI package version

.. image:: https://anaconda.org/conda-forge/pytablewriter/badges/version.svg :target: https://anaconda.org/conda-forge/pytablewriter :alt: conda-forge package version

.. image:: https://img.shields.io/pypi/pyversions/pytablewriter.svg :target: https://pypi.org/project/pytablewriter/ :alt: Supported Python versions

.. image:: https://img.shields.io/pypi/implementation/pytablewriter.svg :target: https://pypi.org/project/pytablewriter :alt: Supported Python implementations

.. image:: https://github.com/thombashi/pytablewriter/actions/workflows/ci.yml/badge.svg :target: https://github.com/thombashi/pytablewriter/actions/workflows/ci.yml :alt: CI status of Linux/macOS/Windows

.. image:: https://coveralls.io/repos/github/thombashi/pytablewriter/badge.svg?branch=master :target: https://coveralls.io/github/thombashi/pytablewriter?branch=master :alt: Test coverage

.. image:: https://github.com/thombashi/pytablewriter/actions/workflows/github-code-scanning/codeql/badge.svg :target: https://github.com/thombashi/pytablewriter/actions/workflows/github-code-scanning/codeql :alt: CodeQL

Features

  • Write a table in various formats:
    • Text formats:
      • AsciiDoc <https://asciidoc.org/>__
      • CSV / Tab-separated values (TSV) / Space-separated values (SSV)
      • HTML / CSS
      • JSON / Line-delimited JSON(LDJSON) <https://en.wikipedia.org/wiki/JSON_streaming#Line-delimited_JSON>__
      • Labeled Tab-separated Values (LTSV) <http://ltsv.org/>__
      • LaTeX: tabular/array environment
      • Markdown: CommonMark / GitHub Flavored Markdown (GFM) <https://github.github.com/gfm/>__ / kramdown <https://kramdown.gettalong.org/>__
      • MediaWiki <https://www.mediawiki.org/wiki/MediaWiki>__
      • reStructuredText: Grid Tables <http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#grid-tables>/Simple Tables <http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#simple-tables>/CSV Table <http://docutils.sourceforge.net/docs/ref/rst/directives.html#id4>__
      • Source code (definition of a variable that represents tabular data)
        • JavaScript / NumPy <https://www.numpy.org/>__ (numpy.array <https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html>) / Pandas <https://pandas.pydata.org/> (pandas.DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>__) / Python
      • TOML <https://github.com/toml-lang/toml>__
      • YAML <https://yaml.org/>__
      • Unicode
    • Binary file formats:
      • Microsoft Excel :superscript:TM (.xlsx/.xls file format)
      • pandas.DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>__ pickle file
      • SQLite <https://www.sqlite.org/index.html>__ database
    • Application-specific formats:
      • Elasticsearch <https://www.elastic.co/products/elasticsearch>__
  • Automatic table cell formatting:
    • Alignment
    • Padding
    • Decimal places of numbers
  • Customize table cell styles:
    • Text/Background color
    • Text alignment
    • Font size/weight
    • Thousand separator for numbers: e.g. 1,000/1 000
  • Configure output:
    • Write a table to a stream such as a file/standard-output/string-buffer/Jupyter-Notebook
    • Get rendered tabular text
  • Data sources:
    • nested list
    • CSV
    • pandas.DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>__ / pandas.Series <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html>__
    • etc.
  • Multibyte character support
  • ANSI color support

Installation

Installation: pip

::

pip install pytablewriter

Some of the formats require additional dependency packages, you can install these packages as follows:

.. csv-table:: Installation of optional dependencies :header: Installation example, Remark

``pip install pytablewriter[es]``, Elasticsearch
``pip install pytablewriter[excel]``, Excel
``pip install pytablewriter[html]``, HTML
``pip install pytablewriter[sqlite]``, SQLite database
``pip install pytablewriter[toml]``, TOML
``pip install pytablewriter[theme]``, pytablewriter theme plugins
``pip install pytablewriter[all]``, Install all of the optional dependencies

Installation: conda

::

conda install -c conda-forge pytablewriter

Installation: apt

::

sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-pytablewriter

Examples

Write tables

Write a Markdown table

:Sample Code:
    .. code-block:: python

        from pytablewriter import MarkdownTableWriter

        def main():
            writer = MarkdownTableWriter(
                table_name="example_table",
                headers=["int", "float", "str", "bool", "mix", "time"],
                value_matrix=[
                    [0,   0.1,      "hoge", True,   0,      "2017-01-01 03:04:05+0900"],
                    [2,   "-2.23",  "foo",  False,  None,   "2017-12-23 45:01:23+0900"],
                    [3,   0,        "bar",  "true",  "inf", "2017-03-03 33:44:55+0900"],
                    [-10, -9.9,     "",     "FALSE", "nan", "2017-01-01 00:00:00+0900"],
                ],
            )
            writer.write_table()

        if __name__ == "__main__":
            main()

:Output:
    .. code-block::

        # example_table
        |int|float|str |bool |  mix   |          time          |
        |--:|----:|----|-----|-------:|------------------------|
        |  0| 0.10|hoge|True |       0|2017-01-01 03:04:05+0900|
        |  2|-2.23|foo |False|        |2017-12-23 12:34:51+0900|
        |  3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900|
        |-10|-9.90|    |False|     NaN|2017-01-01 00:00:00+0900|

:Rendering Result:
    .. figure:: https://cdn.jsdelivr.net/gh/thombashi/pytablewriter@master/docs/pages/examples/table_format/text/ss/markdown.png
       :scale: 80%
       :alt: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/ss/markdown.png

       Rendered markdown at GitHub

Write a Markdown table with margins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:Sample Code:
    .. code-block:: python

        from pytablewriter import MarkdownTableWriter

        def main():
            writer = MarkdownTableWriter(
                table_name="write a table with margins",
                headers=["int", "float", "str", "bool", "mix", "time"],
                value_matrix=[
                    [0,   0.1,      "hoge", True,   0,      "2017-01-01 03:04:05+0900"],
                    [2,   "-2.23",  "foo",  False,  None,   "2017-12-23 45:01:23+0900"],
                    [3,   0,        "bar",  "true",  "inf", "2017-03-03 33:44:55+0900"],
                    [-10, -9.9,     "",     "FALSE", "nan", "2017-01-01 00:00:00+0900"],
                ],
                margin=1  # add a whitespace for both sides of each cell
            )
            writer.write_table()

        if __name__ == "__main__":
            main()

:Output:
    .. code-block::

        # write a table with margins
        | int | float | str  | bool  |   mix    |           time           |
        | --: | ----: | ---- | ----- | -------: | ------------------------ |
        |   0 |  0.10 | hoge | True  |        0 | 2017-01-01 03:04:05+0900 |
        |   2 | -2.23 | foo  | False |          | 2017-12-23 12:34:51+0900 |
        |   3 |  0.00 | bar  | True  | Infinity | 2017-03-03 22:44:55+0900 |
        | -10 | -9.90 |      | False |      NaN | 2017-01-01 00:00:00+0900 |

``margin`` attribute can be available for all of the text format writer classes.

Write a GitHub Flavored Markdown (GFM) table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you set ``flavor`` keyword argument of ``MarkdownTableWriter`` class to ``"github"`` or ``"gfm"``, the writer will output markdown tables with GitHub flavor.
GFM can apply some additional styles to tables such as ``fg_color`` (text color).

:Sample Code:
    .. code-block:: python

            from pytablewriter import MarkdownTableWriter
            from pytablewriter.style import Style

            writer = MarkdownTableWriter(
                column_styles=[
                    Style(fg_color="red"),
                    Style(fg_color="green", decoration_line="underline"),
                ],
                headers=["A", "B"],
                value_matrix=[
                    ["abc", 1],
                    ["efg", 2],
                ],
                margin=1,
                flavor="github",
                enable_ansi_escape=False,
            )
            writer.write_table()

Rendered results can be found at `here <https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/output/markdown/gfm.md>`__

Apply styles to GFM table with programmatically
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Applying style filters to GFM allows for more flexible style settings for cells.
See also the `example <#style-filter>`_

Write a Markdown table to a stream or a file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`Refer an example <https://github.com/thombashi/pytablewriter/blob/master/examp
View on GitHub
GitHub Stars631
CategoryData
Updated8d ago
Forks46

Languages

Python

Security Score

100/100

Audited on Mar 17, 2026

No findings