0
# Styling and Theming
1
2
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.
3
4
## Capabilities
5
6
### Rule Style Enumerations
7
8
Control horizontal and vertical rule display in tables.
9
10
```python { .api }
11
class HRuleStyle(IntEnum):
12
"""Horizontal rule display styles."""
13
FRAME = 0 # Rules only around table frame
14
ALL = 1 # Rules after every row
15
NONE = 2 # No horizontal rules
16
HEADER = 3 # Rule only after header
17
18
class VRuleStyle(IntEnum):
19
"""Vertical rule display styles."""
20
FRAME = 0 # Rules only around table frame
21
ALL = 1 # Rules between every column
22
NONE = 2 # No vertical rules
23
```
24
25
Usage examples:
26
27
```python
28
from prettytable import HRuleStyle, VRuleStyle
29
30
# Set horizontal rules
31
table.hrules = HRuleStyle.ALL # Rules after every row
32
table.hrules = HRuleStyle.HEADER # Rule only after header
33
table.hrules = HRuleStyle.NONE # No horizontal rules
34
35
# Set vertical rules
36
table.vrules = VRuleStyle.ALL # Rules between columns
37
table.vrules = VRuleStyle.FRAME # Rules only on sides
38
table.vrules = VRuleStyle.NONE # No vertical rules
39
```
40
41
### Table Style Enumeration
42
43
Predefined table formatting styles for common use cases.
44
45
```python { .api }
46
class TableStyle(IntEnum):
47
"""Predefined table formatting styles."""
48
DEFAULT = 10 # Standard table style
49
MSWORD_FRIENDLY = 11 # MS Word compatible style
50
PLAIN_COLUMNS = 12 # Plain column layout
51
MARKDOWN = 13 # Markdown table format
52
ORGMODE = 14 # Org-mode table format
53
DOUBLE_BORDER = 15 # Double-line border style
54
SINGLE_BORDER = 16 # Single-line border style
55
RANDOM = 20 # Randomized styling
56
```
57
58
Usage examples:
59
60
```python
61
from prettytable import TableStyle
62
63
# Apply predefined styles
64
table.set_style(TableStyle.MARKDOWN)
65
table.set_style(TableStyle.DOUBLE_BORDER)
66
table.set_style(TableStyle.PLAIN_COLUMNS)
67
table.set_style(TableStyle.ORGMODE)
68
```
69
70
### Color Table Support
71
72
Extended table with ANSI color theming support for enhanced visual presentation.
73
74
```python { .api }
75
class ColorTable(PrettyTable):
76
"""Extended PrettyTable with color theming support."""
77
78
def __init__(self, field_names=None, **kwargs):
79
"""
80
Initialize colored table with optional theme.
81
82
Parameters:
83
- field_names: Column headers (list or sequence)
84
- theme: Theme object for coloring
85
- **kwargs: Standard PrettyTable options
86
"""
87
88
def get_string(self, **kwargs) -> str:
89
"""Get colored ASCII representation with proper ANSI reset."""
90
91
def update_theme(self) -> None:
92
"""Apply current theme to table characters."""
93
94
@property
95
def theme(self) -> 'Theme':
96
"""Current color theme."""
97
98
@theme.setter
99
def theme(self, value: 'Theme') -> None: ...
100
```
101
102
Usage examples:
103
104
```python
105
from prettytable.colortable import ColorTable, Themes
106
107
# Create colored table
108
color_table = ColorTable(["Name", "Score", "Grade"])
109
110
# Set predefined theme
111
color_table.theme = Themes.OCEAN
112
color_table.theme = Themes.EARTH
113
color_table.theme = Themes.HIGH_CONTRAST
114
115
# Get colored output
116
colored_output = color_table.get_string()
117
print(colored_output)
118
```
119
120
### Theme System
121
122
Color theme configuration for ColorTable styling.
123
124
```python { .api }
125
class Theme:
126
"""Color theme configuration."""
127
128
def __init__(
129
self,
130
default_color: str = "",
131
vertical_char: str = "|",
132
vertical_color: str = "",
133
horizontal_char: str = "-",
134
horizontal_color: str = "",
135
junction_char: str = "+",
136
junction_color: str = ""
137
) -> None:
138
"""
139
Initialize color theme.
140
141
Parameters:
142
- default_color: Default text color (ANSI code)
143
- vertical_char: Character for vertical lines
144
- vertical_color: Color for vertical lines (ANSI code)
145
- horizontal_char: Character for horizontal lines
146
- horizontal_color: Color for horizontal lines (ANSI code)
147
- junction_char: Character for line junctions
148
- junction_color: Color for line junctions (ANSI code)
149
"""
150
151
@staticmethod
152
def format_code(s: str) -> str:
153
"""
154
Format ANSI color code for terminal output.
155
156
Parameters:
157
- s: Color code string
158
159
Returns:
160
Formatted ANSI escape sequence
161
"""
162
```
163
164
### Predefined Themes
165
166
Built-in color themes for common use cases.
167
168
```python { .api }
169
class Themes:
170
"""Container for predefined color themes."""
171
172
DEFAULT: Theme # Default theme with no colors
173
DYSLEXIA_FRIENDLY: Theme # High-contrast theme for accessibility
174
EARTH: Theme # Earth-tone color scheme
175
GLARE_REDUCTION: Theme # Reduced brightness theme
176
HIGH_CONTRAST: Theme # High-contrast black and white theme
177
LAVENDER: Theme # Soft purple color scheme
178
OCEAN: Theme # Blue ocean-inspired theme
179
OCEAN_DEEP: Theme # Deep blue ocean theme
180
PASTEL: Theme # Soft pastel color scheme
181
182
# Color utility constants
183
RESET_CODE: str # ANSI reset code to clear colors
184
```
185
186
Usage examples:
187
188
```python
189
from prettytable.colortable import ColorTable, Themes, Theme, RESET_CODE
190
191
# Use predefined themes
192
table = ColorTable(["Name", "Score"])
193
table.theme = Themes.OCEAN
194
table.theme = Themes.HIGH_CONTRAST
195
table.theme = Themes.EARTH
196
197
# Create custom theme
198
custom_theme = Theme(
199
default_color="\033[0;37m", # White text
200
vertical_color="\033[0;34m", # Blue vertical lines
201
horizontal_color="\033[0;32m", # Green horizontal lines
202
junction_color="\033[0;31m" # Red junctions
203
)
204
table.theme = custom_theme
205
206
# Use ANSI reset code
207
print(table.get_string() + RESET_CODE)
208
209
# Custom theme with different characters
210
fancy_theme = Theme(
211
vertical_char="║",
212
horizontal_char="═",
213
junction_char="╬",
214
vertical_color="\033[1;36m", # Bright cyan
215
horizontal_color="\033[1;33m", # Bright yellow
216
junction_color="\033[1;35m" # Bright magenta
217
)
218
table.theme = fancy_theme
219
```
220
221
### Alignment and Formatting
222
223
Fine-grained control over text alignment and formatting options.
224
225
```python { .api }
226
# Type aliases for alignment and formatting
227
from typing import Literal
228
229
AlignType = Literal["l", "c", "r"] # Left, center, right
230
VAlignType = Literal["t", "m", "b"] # Top, middle, bottom
231
HeaderStyleType = Literal["cap", "title", "upper", "lower", None]
232
```
233
234
Usage examples:
235
236
```python
237
# Column alignment
238
table.align["Name"] = "l" # Left align
239
table.align["Score"] = "r" # Right align
240
table.align["Comment"] = "c" # Center align
241
242
# Vertical alignment (for multi-line cells)
243
table.valign["Description"] = "t" # Top align
244
table.valign["Notes"] = "m" # Middle align
245
table.valign["Address"] = "b" # Bottom align
246
247
# Header styling
248
table.header_style = "title" # Title case headers
249
table.header_style = "upper" # Uppercase headers
250
table.header_style = "lower" # Lowercase headers
251
table.header_style = "cap" # Capitalize first letter
252
```