PrettyTable
PrettyTable is a Python library for easily displaying tabular data in a visually appealing ASCII table format. It supports various output formats including ASCII, HTML, CSV, JSON, LaTeX, and MediaWiki. The library is actively maintained with regular releases, currently at version 3.17.0.
Warnings
- breaking PrettyTable 3.17.0 dropped support for Python 3.9. Users on Python 3.9 or older should use an earlier version of PrettyTable (e.g., 3.16.0 or below) or upgrade their Python interpreter.
- deprecated The direct constants `hrule` and `tableStyle` were deprecated in version 3.12.0. Use the corresponding enum values from `HRuleStyle`, `VRuleStyle`, and `TableStyle` instead for clarity and type safety.
- gotcha `ModuleNotFoundError: No module named 'prettytable'` is a common issue, often caused by installing `prettytable` in a different Python environment (e.g., virtual environment) than the one your IDE or script is using.
- gotcha Prior to 3.15.1, `add_rows()` could raise an `IndexError` if provided with an empty list that was not handled gracefully.
- gotcha In versions prior to 3.14.0, specifying `sortby` directly at class declaration might not have been correctly applied, requiring it to be set after instantiation or via `get_string()` arguments.
Install
-
pip install prettytable
Imports
- PrettyTable
from prettytable import PrettyTable
- ColorTable
from prettytable.colortable import ColorTable
- TableStyle
from prettytable import TableStyle
Quickstart
from prettytable import PrettyTable table = PrettyTable() table.field_names = ["City name", "Area", "Population"] table.add_row(["Adelaide", 1295, 1158259]) table.add_row(["Brisbane", 5905, 1857594]) table.add_row(["Sydney", 2058, 4336374]) print(table) # Example with alignment and style table.align["City name"] = "l" table.align["Population"] = "r" # To use custom styles, import TableStyle from prettytable import TableStyle table.set_style(TableStyle.MARKDOWN) print(table)