PyTableWriter
pytablewriter is a Python library (current version 1.2.1) designed to efficiently write tabular data into a wide array of formats, including AsciiDoc, CSV, Elasticsearch, HTML, JavaScript, JSON, LaTeX, Markdown, MediaWiki, NumPy, Excel, Pandas, Python, reStructuredText, SQLite, TOML, TSV, and YAML. It maintains an active development pace with frequent minor and patch releases, ensuring broad compatibility and feature enhancements.
Warnings
- breaking Python 3.7 and 3.8 are no longer supported as of `pytablewriter` v1.2.1. Python 3.6 was dropped in v1.0.0. Ensure your Python environment is 3.9 or higher.
- breaking Support for Elasticsearch 7 was dropped in `pytablewriter` v1.0.0. If you are using the Elasticsearch writer, you must upgrade your Elasticsearch instance to version 8 or later.
- gotcha Many table formats (e.g., Excel, Pandas, Elasticsearch, SQLite, TOML, specific themes) require additional dependencies that are not installed by default. These are provided as 'extras' for `pip install`.
- gotcha The `Cell` class became immutable in `pytablewriter` v1.0.0. Direct modification of `Cell` objects after creation is no longer possible.
- gotcha Theme plugins, such as `pytablewriter-altrow-theme`, require a separate installation via `pip install pytablewriter[theme]`. Simply specifying a theme name in the writer constructor without the plugin will result in a `RuntimeError`.
Install
-
pip install pytablewriter -
pip install pytablewriter[all] -
pip install pytablewriter[excel] pytablewriter[pandas]
Imports
- MarkdownTableWriter
from pytablewriter import MarkdownTableWriter
- CsvTableWriter
from pytablewriter import CsvTableWriter
- TableWriterFactory
from pytablewriter import TableWriterFactory
Quickstart
import pytablewriter as ptw
import sys
# Create a MarkdownTableWriter instance
writer = ptw.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"],
],
output_stream=sys.stdout # Direct output to stdout
)
# Write the table
writer.write_table()
print("\n--- Using TableWriterFactory ---")
# Alternatively, use the factory to create a writer
writer_from_factory = ptw.TableWriterFactory.create_from_format_name(
"html",
headers=["Key", "Value"],
value_matrix=[["API_KEY", "***"], ["Endpoint", "https://example.com/api"]],
output_stream=sys.stdout # Direct output to stdout
)
writer_from_factory.write_table()