A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
—
Comprehensive export and output formatting capabilities for generating tables in multiple formats. Supports ASCII text, HTML, CSV, JSON, LaTeX, MediaWiki markup, and pagination for various use cases.
Generate formatted ASCII text representation with extensive customization options.
def get_string(self, **kwargs) -> str:
"""
Get ASCII text representation of the table.
Parameters:
- start: First row to include (0-based)
- end: Last row to include (0-based)
- fields: List of field names to include
- title: Override table title
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
- hrules: Horizontal rule style override
- vrules: Vertical rule style override
- int_format: Format string for integers
- float_format: Format string for floats
- padding_width: Cell padding width
- left_padding_width: Left padding width
- right_padding_width: Right padding width
- vertical_char: Character for vertical lines
- horizontal_char: Character for horizontal lines
- junction_char: Character for line junctions
- header_style: Header text style
- align: Alignment override
- valign: Vertical alignment override
- max_width: Maximum column width
- min_width: Minimum column width
- max_table_width: Maximum total table width
- min_table_width: Minimum total table width
- oldsortslice: Use old sort/slice behavior
Returns:
Formatted ASCII table string
"""
def __str__(self) -> str:
"""String representation (calls get_string())."""
def __repr__(self) -> str:
"""String representation (calls get_string())."""Usage examples:
# Basic string output
table_str = table.get_string()
print(table_str)
# Output subset of rows and columns
subset = table.get_string(start=1, end=5, fields=["Name", "Age"])
# Sorted output
sorted_table = table.get_string(sortby="Age", reversesort=True)
# Custom formatting
formatted = table.get_string(
int_format="03d",
float_format=".2f",
padding_width=3
)
# Using string conversion
print(str(table))Generate HTML table markup with styling and formatting options.
def get_html_string(self, **kwargs) -> str:
"""
Get HTML representation of the table.
Parameters:
- start: First row to include
- end: Last row to include
- fields: List of field names to include
- title: Override table title
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
- escape: Escape HTML characters in data
- format: Apply CSS formatting
- table_id: HTML table id attribute
- border: Add border attribute
- hrules: Horizontal rule style
- vrules: Vertical rule style
Returns:
HTML table string
"""
def _repr_html_(self) -> str:
"""Jupyter notebook HTML representation."""Usage examples:
# Basic HTML output
html_table = table.get_html_string()
# HTML with custom formatting
html_formatted = table.get_html_string(
format=True,
table_id="data-table",
border=1
)
# HTML subset
html_subset = table.get_html_string(fields=["Name", "Score"])Export table data as comma-separated values with delimiter options.
def get_csv_string(self, **kwargs) -> str:
"""
Get CSV representation of the table.
Parameters:
- start: First row to include
- end: Last row to include
- fields: List of field names to include
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
- delimiter: Field delimiter character
- header: Include header row
Returns:
CSV formatted string
"""Usage examples:
# Standard CSV output
csv_data = table.get_csv_string()
# CSV with custom delimiter
tsv_data = table.get_csv_string(delimiter='\t')
# CSV without header
data_only = table.get_csv_string(header=False)Export table data as structured JSON with various formatting options.
def get_json_string(self, **kwargs) -> str:
"""
Get JSON representation of the table.
Parameters:
- start: First row to include
- end: Last row to include
- fields: List of field names to include
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
- indent: JSON indentation level
- separators: JSON separators tuple
Returns:
JSON formatted string
"""Usage examples:
# Compact JSON
json_compact = table.get_json_string()
# Pretty-printed JSON
json_pretty = table.get_json_string(indent=2)
# JSON subset
json_subset = table.get_json_string(fields=["Name", "Score"])Generate LaTeX table markup for document integration.
def get_latex_string(self, **kwargs) -> str:
"""
Get LaTeX representation of the table.
Parameters:
- start: First row to include
- end: Last row to include
- fields: List of field names to include
- title: Override table title
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
Returns:
LaTeX table string
"""Usage example:
# LaTeX table
latex_table = table.get_latex_string()Generate MediaWiki table markup for wiki integration.
def get_mediawiki_string(self, **kwargs) -> str:
"""
Get MediaWiki representation of the table.
Parameters:
- start: First row to include
- end: Last row to include
- fields: List of field names to include
- title: Override table title
- sortby: Field name to sort by
- reversesort: Reverse sort order
- print_empty: Print table even if no data rows
Returns:
MediaWiki table markup string
"""Usage example:
# MediaWiki table
wiki_table = table.get_mediawiki_string()Unified interface for multiple output formats.
def get_formatted_string(self, out_format: str, **kwargs) -> str:
"""
Get formatted output in specified format.
Parameters:
- out_format: Output format ("text", "html", "csv", "json", "latex", "mediawiki")
- **kwargs: Format-specific options
Returns:
Formatted string in requested format
"""Usage examples:
# Get HTML via generic interface
html_output = table.get_formatted_string("html", format=True)
# Get CSV via generic interface
csv_output = table.get_formatted_string("csv", delimiter=';')
# Dynamic format selection
format_type = "json"
output = table.get_formatted_string(format_type, indent=2)Break large tables into pages for better readability.
def paginate(self, page_length: int = 58, line_break: str = '\f', **kwargs) -> str:
"""
Get paginated string output.
Parameters:
- page_length: Number of lines per page
- line_break: Character sequence for page breaks
- **kwargs: Options passed to get_string()
Returns:
Paginated table string with page breaks
"""Usage examples:
# Standard pagination
paginated = table.paginate()
# Custom page length
paginated_short = table.paginate(page_length=30)
# Custom page break character
paginated_custom = table.paginate(line_break='\n--- PAGE BREAK ---\n')Install with Tessl CLI
npx tessl i tessl/pypi-prettytable