0
# PrettyTable
1
2
A 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.
3
4
## Package Information
5
6
- **Package Name**: prettytable
7
- **Language**: Python
8
- **Installation**: `pip install prettytable`
9
10
## Core Imports
11
12
```python
13
from prettytable import PrettyTable
14
```
15
16
For color support:
17
18
```python
19
from prettytable.colortable import ColorTable, Themes, Theme, RESET_CODE
20
```
21
22
For factory functions:
23
24
```python
25
from prettytable import from_csv, from_json, from_html, from_db_cursor
26
```
27
28
For styling enums:
29
30
```python
31
from prettytable import HRuleStyle, VRuleStyle, TableStyle
32
```
33
34
## Basic Usage
35
36
```python
37
from prettytable import PrettyTable
38
39
# Create a new table
40
table = PrettyTable()
41
42
# Set field names (column headers)
43
table.field_names = ["City", "Area", "Population", "Annual Rainfall"]
44
45
# Add rows
46
table.add_row(["Adelaide", 1295, 1158259, 600.5])
47
table.add_row(["Brisbane", 5905, 2074003, 1146.4])
48
table.add_row(["Darwin", 112, 120900, 1714.7])
49
table.add_row(["Hobart", 1357, 205556, 619.5])
50
51
# Display the table
52
print(table)
53
54
# Customize alignment
55
table.align["City"] = "l" # Left align
56
table.align["Population"] = "r" # Right align
57
58
# Set table style
59
table.set_style(TableStyle.MARKDOWN)
60
print(table)
61
```
62
63
## Architecture
64
65
PrettyTable follows a straightforward object-oriented design:
66
67
- **PrettyTable**: Core table class handling data storage, formatting, and output generation
68
- **ColorTable**: Extended table class adding ANSI color theming support
69
- **Factory Functions**: Utilities for creating tables from various data sources (CSV, JSON, HTML, databases)
70
- **Styling Enums**: Type-safe styling options for borders, rules, and table styles
71
- **Theme System**: Color theming infrastructure for enhanced visual presentation
72
73
The library supports multiple output formats (ASCII, HTML, CSV, JSON, LaTeX, MediaWiki) and provides extensive customization options for alignment, borders, spacing, and visual styling.
74
75
## Capabilities
76
77
### Core Table Operations
78
79
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.
80
81
```python { .api }
82
class PrettyTable:
83
def __init__(self, field_names: list[str] | None = None, **kwargs): ...
84
def add_row(self, row: list[Any], *, divider: bool = False) -> None: ...
85
def add_rows(self, rows: list[list[Any]], *, divider: bool = False) -> None: ...
86
def add_column(self, fieldname: str, column: list[Any], align: str = 'c', valign: str = 't') -> None: ...
87
def del_row(self, row_index: int) -> None: ...
88
def del_column(self, fieldname: str) -> None: ...
89
def clear_rows(self) -> None: ...
90
def clear(self) -> None: ...
91
def copy(self) -> 'PrettyTable': ...
92
```
93
94
[Core Table Operations](./core-table.md)
95
96
### Output Formats
97
98
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.
99
100
```python { .api }
101
def get_string(self, **kwargs) -> str: ...
102
def get_html_string(self, **kwargs) -> str: ...
103
def get_csv_string(self, **kwargs) -> str: ...
104
def get_json_string(self, **kwargs) -> str: ...
105
def get_latex_string(self, **kwargs) -> str: ...
106
def get_mediawiki_string(self, **kwargs) -> str: ...
107
def paginate(self, page_length: int = 58, line_break: str = '\f', **kwargs) -> str: ...
108
```
109
110
[Output Formats](./output-formats.md)
111
112
### Styling and Theming
113
114
Comprehensive styling system including border styles, alignment options, color theming, and predefined table styles. Supports both ASCII and colored output with extensive customization capabilities.
115
116
```python { .api }
117
class HRuleStyle(IntEnum):
118
FRAME: int
119
ALL: int
120
NONE: int
121
HEADER: int
122
123
class VRuleStyle(IntEnum):
124
FRAME: int
125
ALL: int
126
NONE: int
127
128
class TableStyle(IntEnum):
129
DEFAULT: int
130
MARKDOWN: int
131
ORGMODE: int
132
DOUBLE_BORDER: int
133
# ... additional styles
134
```
135
136
[Styling and Theming](./styling.md)
137
138
### Factory Functions
139
140
Utilities 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.
141
142
```python { .api }
143
def from_csv(fp, field_names: list[str] | None = None, **kwargs) -> PrettyTable: ...
144
def from_json(json_string: str, **kwargs) -> PrettyTable: ...
145
def from_html(html_code: str, **kwargs) -> list[PrettyTable]: ...
146
def from_html_one(html_code: str, **kwargs) -> PrettyTable: ...
147
def from_db_cursor(cursor, **kwargs) -> PrettyTable | None: ...
148
def from_mediawiki(wiki_text: str, **kwargs) -> PrettyTable: ...
149
```
150
151
[Factory Functions](./factory-functions.md)
152
153
## Types
154
155
```python { .api }
156
from typing import Any, Literal
157
from collections.abc import Sequence
158
159
RowType = list[Any]
160
AlignType = Literal["l", "c", "r"]
161
VAlignType = Literal["t", "m", "b"]
162
HeaderStyleType = Literal["cap", "title", "upper", "lower", None]
163
```
164
165
## Deprecated Constants
166
167
For backward compatibility, the following constants are still available but deprecated. Use the corresponding enum values instead:
168
169
```python { .api }
170
# Deprecated constants (use HRuleStyle/VRuleStyle/TableStyle enums instead)
171
ALL: int # Use HRuleStyle.ALL or VRuleStyle.ALL
172
DEFAULT: int # Use TableStyle.DEFAULT
173
DOUBLE_BORDER: int # Use TableStyle.DOUBLE_BORDER
174
FRAME: int # Use HRuleStyle.FRAME or VRuleStyle.FRAME
175
HEADER: int # Use HRuleStyle.HEADER
176
MARKDOWN: int # Use TableStyle.MARKDOWN
177
MSWORD_FRIENDLY: int # Use TableStyle.MSWORD_FRIENDLY
178
NONE: int # Use HRuleStyle.NONE or VRuleStyle.NONE
179
ORGMODE: int # Use TableStyle.ORGMODE
180
PLAIN_COLUMNS: int # Use TableStyle.PLAIN_COLUMNS
181
RANDOM: int # Use TableStyle.RANDOM
182
SINGLE_BORDER: int # Use TableStyle.SINGLE_BORDER
183
```
184
185
**Note**: These constants trigger deprecation warnings when accessed.