ansi2txt

0.2.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

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

view raw JSON →