A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
—
Comprehensive styling system for table appearance including border styles, alignment options, color theming with ANSI codes, and predefined table styles. Supports both ASCII and colored output with extensive customization.
Control horizontal and vertical rule display in tables.
class HRuleStyle(IntEnum):
"""Horizontal rule display styles."""
FRAME = 0 # Rules only around table frame
ALL = 1 # Rules after every row
NONE = 2 # No horizontal rules
HEADER = 3 # Rule only after header
class VRuleStyle(IntEnum):
"""Vertical rule display styles."""
FRAME = 0 # Rules only around table frame
ALL = 1 # Rules between every column
NONE = 2 # No vertical rulesUsage examples:
from prettytable import HRuleStyle, VRuleStyle
# Set horizontal rules
table.hrules = HRuleStyle.ALL # Rules after every row
table.hrules = HRuleStyle.HEADER # Rule only after header
table.hrules = HRuleStyle.NONE # No horizontal rules
# Set vertical rules
table.vrules = VRuleStyle.ALL # Rules between columns
table.vrules = VRuleStyle.FRAME # Rules only on sides
table.vrules = VRuleStyle.NONE # No vertical rulesPredefined table formatting styles for common use cases.
class TableStyle(IntEnum):
"""Predefined table formatting styles."""
DEFAULT = 10 # Standard table style
MSWORD_FRIENDLY = 11 # MS Word compatible style
PLAIN_COLUMNS = 12 # Plain column layout
MARKDOWN = 13 # Markdown table format
ORGMODE = 14 # Org-mode table format
DOUBLE_BORDER = 15 # Double-line border style
SINGLE_BORDER = 16 # Single-line border style
RANDOM = 20 # Randomized stylingUsage examples:
from prettytable import TableStyle
# Apply predefined styles
table.set_style(TableStyle.MARKDOWN)
table.set_style(TableStyle.DOUBLE_BORDER)
table.set_style(TableStyle.PLAIN_COLUMNS)
table.set_style(TableStyle.ORGMODE)Extended table with ANSI color theming support for enhanced visual presentation.
class ColorTable(PrettyTable):
"""Extended PrettyTable with color theming support."""
def __init__(self, field_names=None, **kwargs):
"""
Initialize colored table with optional theme.
Parameters:
- field_names: Column headers (list or sequence)
- theme: Theme object for coloring
- **kwargs: Standard PrettyTable options
"""
def get_string(self, **kwargs) -> str:
"""Get colored ASCII representation with proper ANSI reset."""
def update_theme(self) -> None:
"""Apply current theme to table characters."""
@property
def theme(self) -> 'Theme':
"""Current color theme."""
@theme.setter
def theme(self, value: 'Theme') -> None: ...Usage examples:
from prettytable.colortable import ColorTable, Themes
# Create colored table
color_table = ColorTable(["Name", "Score", "Grade"])
# Set predefined theme
color_table.theme = Themes.OCEAN
color_table.theme = Themes.EARTH
color_table.theme = Themes.HIGH_CONTRAST
# Get colored output
colored_output = color_table.get_string()
print(colored_output)Color theme configuration for ColorTable styling.
class Theme:
"""Color theme configuration."""
def __init__(
self,
default_color: str = "",
vertical_char: str = "|",
vertical_color: str = "",
horizontal_char: str = "-",
horizontal_color: str = "",
junction_char: str = "+",
junction_color: str = ""
) -> None:
"""
Initialize color theme.
Parameters:
- default_color: Default text color (ANSI code)
- vertical_char: Character for vertical lines
- vertical_color: Color for vertical lines (ANSI code)
- horizontal_char: Character for horizontal lines
- horizontal_color: Color for horizontal lines (ANSI code)
- junction_char: Character for line junctions
- junction_color: Color for line junctions (ANSI code)
"""
@staticmethod
def format_code(s: str) -> str:
"""
Format ANSI color code for terminal output.
Parameters:
- s: Color code string
Returns:
Formatted ANSI escape sequence
"""Built-in color themes for common use cases.
class Themes:
"""Container for predefined color themes."""
DEFAULT: Theme # Default theme with no colors
DYSLEXIA_FRIENDLY: Theme # High-contrast theme for accessibility
EARTH: Theme # Earth-tone color scheme
GLARE_REDUCTION: Theme # Reduced brightness theme
HIGH_CONTRAST: Theme # High-contrast black and white theme
LAVENDER: Theme # Soft purple color scheme
OCEAN: Theme # Blue ocean-inspired theme
OCEAN_DEEP: Theme # Deep blue ocean theme
PASTEL: Theme # Soft pastel color scheme
# Color utility constants
RESET_CODE: str # ANSI reset code to clear colorsUsage examples:
from prettytable.colortable import ColorTable, Themes, Theme, RESET_CODE
# Use predefined themes
table = ColorTable(["Name", "Score"])
table.theme = Themes.OCEAN
table.theme = Themes.HIGH_CONTRAST
table.theme = Themes.EARTH
# Create custom theme
custom_theme = Theme(
default_color="\033[0;37m", # White text
vertical_color="\033[0;34m", # Blue vertical lines
horizontal_color="\033[0;32m", # Green horizontal lines
junction_color="\033[0;31m" # Red junctions
)
table.theme = custom_theme
# Use ANSI reset code
print(table.get_string() + RESET_CODE)
# Custom theme with different characters
fancy_theme = Theme(
vertical_char="║",
horizontal_char="═",
junction_char="╬",
vertical_color="\033[1;36m", # Bright cyan
horizontal_color="\033[1;33m", # Bright yellow
junction_color="\033[1;35m" # Bright magenta
)
table.theme = fancy_themeFine-grained control over text alignment and formatting options.
# Type aliases for alignment and formatting
from typing import Literal
AlignType = Literal["l", "c", "r"] # Left, center, right
VAlignType = Literal["t", "m", "b"] # Top, middle, bottom
HeaderStyleType = Literal["cap", "title", "upper", "lower", None]Usage examples:
# Column alignment
table.align["Name"] = "l" # Left align
table.align["Score"] = "r" # Right align
table.align["Comment"] = "c" # Center align
# Vertical alignment (for multi-line cells)
table.valign["Description"] = "t" # Top align
table.valign["Notes"] = "m" # Middle align
table.valign["Address"] = "b" # Bottom align
# Header styling
table.header_style = "title" # Title case headers
table.header_style = "upper" # Uppercase headers
table.header_style = "lower" # Lowercase headers
table.header_style = "cap" # Capitalize first letterInstall with Tessl CLI
npx tessl i tessl/pypi-prettytable