PyTableWriter

1.2.1 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This example demonstrates how to create and write a simple Markdown table to standard output using `MarkdownTableWriter`. It also shows how to use the `TableWriterFactory` to dynamically create a writer for a specified format, such as HTML. The `output_stream` is set to `sys.stdout` for direct console output, though it can also be a file object or `io.StringIO` to capture the output as a string.

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()

view raw JSON →