A Python module for formatting text with ANSI escape codes.
pip install fansi_text from fansi_text import Red print(Red("Red text"))from fansi_text import RGBColor print(RGBColor("RGB Text", 200, 130, 60))You can also form complex combinations of overlapping effects.
import fansi_text as ft print( ft.Bold("Bold, " + ft.Green("green and bold. ")) + "Then normal text. Then " + ft.Underline(ft.Overline("under and over lined. ")) + "Next " + ft.Italic("italics. Add " + ft.Yellow("yellow " + ft.Bold("and bold too! ") + "Then back to yellow and italics, ") + "then just italics, ") + "and finally back to normal." )A demo_all method is provided to allow for testing all supported colours and effects in your terminal.
import fansi_text as ft ft.demo_all()The full list of colours and effects are shown below (notes per Wikipedia). Note that support for each code varies between terminals.
| ANSI Code | Name | Example | Note |
|---|---|---|---|
| 0 | Reset | All attributes become turned off | |
| 1 | Bold | Bold Text | As with faint, the color change is a PC (SCO / CGA) invention. |
| 2 | Faint | Faint Text | May be implemented as a light font weight like bold. |
| 3 | Italic | Italic Text | Not widely supported. Sometimes treated as inverse or blink. |
| 4 | Underline | Underlined Text | Style extensions exist for Kitty, VTE, mintty, iTerm2 and Konsole. |
| 5 | SlowBlink | (Example not supported in README) | Sets blinking to less than 150 times per minute |
| 6 | FastBlink | (Example not supported in README) | MS-DOS ANSI.SYS, 150+ per minute; not widely supported |
| 7 | Invert | Inverted | Swap foreground and background colors; inconsistent emulation |
| 8 | Conceal | Not widely supported. | |
| 9 | Strikethrough | Characters legible but marked as if for deletion. Not supported in Terminal.app. | |
| 10 | Font0 | - | Default Font |
| 11 | Font1 | - | Select alternative font n − 10 |
| 12 | Font2 | - | Select alternative font n − 10 |
| 13 | Font3 | - | Select alternative font n − 10 |
| 14 | Font4 | - | Select alternative font n − 10 |
| 15 | Font5 | - | Select alternative font n − 10 |
| 16 | Font6 | - | Select alternative font n − 10 |
| 17 | Font7 | - | Select alternative font n − 10 |
| 18 | Font8 | - | Select alternative font n − 10 |
| 19 | Font9 | - | Select alternative font n − 10 |
| 20 | FrakturFont | (Example not supported in README) | Rarely supported |
| 21 | DoubleUnderlined | Double Underline | Double-underline per ECMA-48 but often disables bold intensity. |
| 26 | ProportionalSpacing | - | ITU T.61 and T.416, not known to be used on terminals |
| 30 | Black | Black Text | Set text colour to black |
| 31 | Red | Red Text | Set text colour to red |
| 32 | Green | Green Text | Set text colour to green |
| 33 | Yellow | Yellow Text | Set text colour to yellow |
| 34 | Blue | Blue Text | Set text colour to blue |
| 35 | Magenta | Magenta Text | Set text colour to magenta |
| 36 | Cyan | Cyan Text | Set text colour to cyan |
| 37 | White | White Text | Set text colour to white |
| 38 | RGBColor | RGBColor(172,100,112) | Use with parameters e.g. RGBColor('Text', 200, 130, 60) |
| 40 | BgBlack | Text | Set black background |
| 41 | BgRed | Text | Set red background |
| 42 | BgGreen | Text | Set green background |
| 43 | BgYellow | Text | Set yellow background |
| 44 | BgBlue | Text | Set blue background |
| 45 | BgMagenta | Text | Set magenta background |
| 46 | BgCyan | Text | Set cyan background |
| 47 | BgWhite | Text | Set white background |
| 48 | RGBBgColor | RGBBgColor(172,100,112) | Use with parameters e.g. RBGBgColor('Text', 200, 130, 60) |
| 51 | Framed | Framed Text | Implemented as "emoji variation selector" in mintty. |
| 52 | Encircled | Encircled Text | |
| 53 | Overlined | Overlined Text | Not supported in Terminal.app |
| 58 | UnderlineColor | (Example not supported in README) | Not in standard; implemented in Kitty, VTE, mintty, and iTerm2. Next arguments are 5;n or 2;r;g;b. |
| 60–64 | Ideogram effects | (Example not supported in README) | Rarely supported |
| 73 | Superscript | Superscript | Implemented only in mintty |
| 74 | Subscript | Subscript | |
| 90 | BrightBlack | Bright Black Text | Set text colour to bright black (gray) |
| 91 | BrightRed | Bright Red Text | Set text colour to bright red |
| 92 | BrightGreen | Bright Green Text | Set text colour to bright green |
| 93 | BrightYellow | Bright Yellow Text | Set text colour to bright yellow |
| 94 | BrightBlue | Bright Blue Text | Set text colour to bright blue |
| 95 | BrightMagenta | Bright Magenta Text | Set text colour to bright magenta |
| 96 | BrightCyan | Bright Cyan Text | Set text colour to bright cyan |
| 97 | BrightWhite | Bright White Text | Set text colour to bright white |
| 100 | BgBrightBlack | Bright Black Background | Set bright black (gray) background |
| 101 | BgBrightRed | Bright Red Background | Set bright red background |
| 102 | BgBrightGreen | Bright Green Background | Set bright green background |
| 103 | BgBrightYellow | Bright Yellow Background | Set bright yellow background |
| 104 | BgBrightBlue | Bright Blue Background | Set bright blue background |
| 105 | BgBrightMagenta | Bright Magenta Background | Set bright magenta background |
| 106 | BgBrightCyan | Bright Cyan Background | Set bright cyan background |
| 107 | BgBrightWhite | Bright White Background | Set bright white background |