pyansistring gives you a string type that keeps styling securely attached while you use familiar, native Python string operations. Whether you are building a simple CLI tool, visualizing data in a complex terminal dashboard, or exporting styled output to the web, pyansistring handles the math and formatting for you.
- ANSIString class that subclasses Python
str. - Style-preserving operations for concatenation, slicing, splitting, joining, replacing, stripping, case transforms, and formatting (f-strings,
.format()). - SGR styling and attributes: bold, dim, italic, underline, strikethrough, invert, and advanced underline modes (single, double, curly, dotted, dashed).
- Color channels: 4-bit ANSI, 8-bit palette, and 24-bit RGB for foreground, background, and underlines.
- Targeting modes: apply to full strings, slice ranges, or word matches (case-sensitive or insensitive).
- Gradient engine: RGB or HSL interpolation, coordinate-based gradients for multiline text, and out-of-bounds handling.
- SVG export: render text or path modes with per-character coloring and optional custom fonts.
- ANSI parsing: convert raw ANSI-encoded strings back into
ANSIStringinstances with styles intact. - Large constants base: extensive predefined color constants and palettes, plus SGR/regex helpers for easy access.
- Python 3.11+
Install the base package:
pip install pyansistringInstall extras when needed:
pip install pyansistring[svg] # For SVG export
pip install pyansistring[all] # Install all optional dependenciesfrom pyansistring import ANSIString, Foreground, Background, SGR
text = (
ANSIString("Hello, World!")
.fg_4b(Foreground.YELLOW)
.bg_4b(Background.BLUE)
.style(SGR.BOLD)
)
print(text)The examples below are generated by examples/generate_usage_svg.py.
from pyansistring import ANSIString
print(ANSIString("Hello, World!"))from pyansistring import ANSIString, Foreground, Background, SGR
print(
ANSIString("Hello, World!")
.fg_4b(Foreground.YELLOW)
.bg_4b(Background.BLUE)
.style(SGR.BOLD)
)from pyansistring import ANSIString, Foreground, Background, SGR
print(
ANSIString("Hello, World!")
.fg_4b(Foreground.YELLOW, (0, 5), (7, 12))
.bg_4b(Background.BLUE, (7, 12))
.style(SGR.BOLD, (7, 12))
)from pyansistring import ANSIString, Foreground, Background, SGR
print(
ANSIString("Hello, World!")
.fg_4b_words(Foreground.YELLOW, "Hello", "World")
.bg_4b_words(Background.BLUE, "World")
.style_words(SGR.BOLD, "Hello", "World")
)from pyansistring import ANSIString, SGR
print(ANSIString("Hello, World!").style(SGR.BOLD).style(SGR.UNDERLINE))from pyansistring import ANSIString, Foreground, Background
print(ANSIString("Hello, World!").fg_4b(Foreground.YELLOW).bg_4b(Background.BLUE))
print(ANSIString("Hello, World!").fg_8b(11).bg_8b(4).ul_8b(74))
print(ANSIString("Hello, World!").fg_24b(255, 255, 0).bg_24b(0, 0, 238).ul_24b(135, 175, 215))from pyansistring import ANSIString, UnderlineMode
print(
ANSIString("Hello, World!")
.bg_24b(255, 255, 255)
.ul_24b(255, 0, 0)
.style(UnderlineMode.DOUBLE)
)from pyansistring import ANSIString
print(ANSIString("Hello, World! This is rainbow text!").rainbow(fg=True))from pyansistring import ANSIString
print(
ANSIString("Hello, World! This is gradient text!")
.gradient([(84, 161, 255), (233, 200, 216)], fg=True)
)
print(
ANSIString("Hello, colorful gradient world!")
.gradient_words([(255, 99, 71), (255, 215, 0)], "Hello", "world", case_sensitive=False, fg=True)
)
print(
ANSIString("HELLO\nworld")
.gradient_coordinates(
[(255, 0, 120), (0, 200, 255)],
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
index_base=1,
fg=True,
)
)from pyansistring import ANSIString
raw = "\x1b[31mError\x1b[0m: file not found"
parsed = ANSIString.from_ansi(raw)
print(parsed.plain_text)
print(parsed)from fontTools.ttLib import TTFont
from pyansistring import ANSIString, SGR
font = TTFont("path/to/font.ttf")
styled = ANSIString("SVG output").style(SGR.BOLD).fg_24b(90, 170, 255)
svg_code = styled.to_svg(
font=font,
font_size_px=16,
convert_text_to_path=False,
)For a complete terminal tour, run examples/showcase.py.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feat/amazing-feature) - Commit your Changes (
git commit -m 'feat: ✨ add some amazing-feature') - Push to the Branch (
git push origin feat/amazing-feature) - Open a Pull Request
Important
If linting or tests fail, make sure to fix those and push the changes.
Distributed under the MIT License. See LICENSE for more information.