ansi2txt

raw JSON →
0.2.0 verified Fri Apr 17 auth: no python

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.

pip install ansi2txt
error ModuleNotFoundError: No module named 'ansi2txt'
cause The `ansi2txt` package has not been installed in the current Python environment.
fix
Run pip install ansi2txt to install the library.
error 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`).
fix
Convert your string to bytes before passing it to the function, e.g., ansi2txt(my_string.encode('utf-8')).
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.
fix Ensure your input is a `bytes` object. If you have a unicode string, encode it first: `ansi2txt(your_string.encode('utf-8'))`.
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.
fix For specific edge cases with complex ANSI sequences, review the source code or consider pre-processing your input to handle specific sequences if they are critical and not stripped correctly.

This quickstart demonstrates how to import the `ansi2txt` function and use it to convert an ANSI-formatted byte string (or encoded unicode string) into plain text, stripping all control characters.

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}")