ansi2txt
ansi2txt is a small Python library designed to convert ANSI escape sequences, commonly found in terminal output, into plain text. It effectively strips out color codes and other formatting, leaving only the readable content. The current version is 0.2.0, and releases are infrequent, indicating a stable, feature-complete library for its specific purpose.
Common errors
-
ModuleNotFoundError: No module named 'ansi2txt'
cause The `ansi2txt` package has not been installed in the current Python environment.fixRun `pip install ansi2txt` to install the library. -
TypeError: argument should be byte string or ASCII string, not str
cause You are passing a Python unicode string (`str`) directly to `ansi2txt` when it expects a byte string (`bytes`).fixConvert your string to bytes before passing it to the function, e.g., `ansi2txt(my_string.encode('utf-8'))`.
Warnings
- gotcha The `ansi2txt` function primarily expects byte strings (`bytes`) as input, reflecting its common use case with terminal output. While it can handle ASCII strings, passing a unicode string (`str`) directly will raise a TypeError if it contains non-ASCII characters or if it's not explicitly encoded.
- gotcha The library is designed for common ANSI SGR (Select Graphic Rendition) codes (colors, bold, underline). It may not fully support more complex or non-standard ANSI sequences, such as cursor positioning, screen clearing, or device control sequences, and might leave them in the output or strip them unexpectedly.
Install
-
pip install ansi2txt
Imports
- ansi2txt
from ansi2txt import ansi2txt
Quickstart
from ansi2txt import ansi2txt
# Example ANSI colored string (bytes)
ansi_text_bytes = b'\x1b[31mHello, \x1b[32mWorld!\x1b[0m This is \x1b[4mcolored\x1b[0m text.'
# Convert to plain text
plain_text = ansi2txt(ansi_text_bytes)
print(f"Original ANSI: {ansi_text_bytes.decode('utf-8')}")
print(f"Plain text: {plain_text}")
# Example with unicode string (needs encoding first)
ansi_text_str = '\u001b[33mWarning:\u001b[0m Something happened.'
plain_text_from_str = ansi2txt(ansi_text_str.encode('utf-8'))
print(f"Plain text from string: {plain_text_from_str}")