A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
npx @tessl/cli install tessl/pypi-prettytable@3.16.0A comprehensive Python library for creating and formatting ASCII tables. PrettyTable provides extensive table creation, data manipulation, styling options, and export capabilities, making it ideal for displaying tabular data in terminal applications, reports, and documentation.
pip install prettytablefrom prettytable import PrettyTableFor color support:
from prettytable.colortable import ColorTable, Themes, Theme, RESET_CODEFor factory functions:
from prettytable import from_csv, from_json, from_html, from_db_cursorFor styling enums:
from prettytable import HRuleStyle, VRuleStyle, TableStylefrom prettytable import PrettyTable
# Create a new table
table = PrettyTable()
# Set field names (column headers)
table.field_names = ["City", "Area", "Population", "Annual Rainfall"]
# Add rows
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 2074003, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])
# Display the table
print(table)
# Customize alignment
table.align["City"] = "l" # Left align
table.align["Population"] = "r" # Right align
# Set table style
table.set_style(TableStyle.MARKDOWN)
print(table)PrettyTable follows a straightforward object-oriented design:
The library supports multiple output formats (ASCII, HTML, CSV, JSON, LaTeX, MediaWiki) and provides extensive customization options for alignment, borders, spacing, and visual styling.
Primary table functionality including creation, data manipulation, field management, and basic formatting options. Handles row and column operations, copying, clearing, and fundamental table structure management.
class PrettyTable:
def __init__(self, field_names: list[str] | None = None, **kwargs): ...
def add_row(self, row: list[Any], *, divider: bool = False) -> None: ...
def add_rows(self, rows: list[list[Any]], *, divider: bool = False) -> None: ...
def add_column(self, fieldname: str, column: list[Any], align: str = 'c', valign: str = 't') -> None: ...
def del_row(self, row_index: int) -> None: ...
def del_column(self, fieldname: str) -> None: ...
def clear_rows(self) -> None: ...
def clear(self) -> None: ...
def copy(self) -> 'PrettyTable': ...Multiple export formats for different use cases including ASCII text, HTML, CSV, JSON, LaTeX, and MediaWiki markup. Supports customizable formatting options, pagination, and integration with various output systems.
def get_string(self, **kwargs) -> str: ...
def get_html_string(self, **kwargs) -> str: ...
def get_csv_string(self, **kwargs) -> str: ...
def get_json_string(self, **kwargs) -> str: ...
def get_latex_string(self, **kwargs) -> str: ...
def get_mediawiki_string(self, **kwargs) -> str: ...
def paginate(self, page_length: int = 58, line_break: str = '\f', **kwargs) -> str: ...Comprehensive styling system including border styles, alignment options, color theming, and predefined table styles. Supports both ASCII and colored output with extensive customization capabilities.
class HRuleStyle(IntEnum):
FRAME: int
ALL: int
NONE: int
HEADER: int
class VRuleStyle(IntEnum):
FRAME: int
ALL: int
NONE: int
class TableStyle(IntEnum):
DEFAULT: int
MARKDOWN: int
ORGMODE: int
DOUBLE_BORDER: int
# ... additional stylesUtilities for creating tables from various data sources including CSV files, JSON data, HTML tables, database cursors, and MediaWiki markup. Simplifies table creation from existing structured data.
def from_csv(fp, field_names: list[str] | None = None, **kwargs) -> PrettyTable: ...
def from_json(json_string: str, **kwargs) -> PrettyTable: ...
def from_html(html_code: str, **kwargs) -> list[PrettyTable]: ...
def from_html_one(html_code: str, **kwargs) -> PrettyTable: ...
def from_db_cursor(cursor, **kwargs) -> PrettyTable | None: ...
def from_mediawiki(wiki_text: str, **kwargs) -> PrettyTable: ...from typing import Any, Literal
from collections.abc import Sequence
RowType = list[Any]
AlignType = Literal["l", "c", "r"]
VAlignType = Literal["t", "m", "b"]
HeaderStyleType = Literal["cap", "title", "upper", "lower", None]For backward compatibility, the following constants are still available but deprecated. Use the corresponding enum values instead:
# Deprecated constants (use HRuleStyle/VRuleStyle/TableStyle enums instead)
ALL: int # Use HRuleStyle.ALL or VRuleStyle.ALL
DEFAULT: int # Use TableStyle.DEFAULT
DOUBLE_BORDER: int # Use TableStyle.DOUBLE_BORDER
FRAME: int # Use HRuleStyle.FRAME or VRuleStyle.FRAME
HEADER: int # Use HRuleStyle.HEADER
MARKDOWN: int # Use TableStyle.MARKDOWN
MSWORD_FRIENDLY: int # Use TableStyle.MSWORD_FRIENDLY
NONE: int # Use HRuleStyle.NONE or VRuleStyle.NONE
ORGMODE: int # Use TableStyle.ORGMODE
PLAIN_COLUMNS: int # Use TableStyle.PLAIN_COLUMNS
RANDOM: int # Use TableStyle.RANDOM
SINGLE_BORDER: int # Use TableStyle.SINGLE_BORDERNote: These constants trigger deprecation warnings when accessed.