{"id":9466,"library":"ansi2txt","title":"ansi2txt","description":"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.","status":"active","version":"0.2.0","language":"en","source_language":"en","source_url":"https://github.com/teythso/ansi2txt","tags":["ansi","terminal","plain-text","color","formatting"],"install":[{"cmd":"pip install ansi2txt","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ansi2txt","correct":"from ansi2txt import ansi2txt"}],"quickstart":{"code":"from ansi2txt import ansi2txt\n\n# Example ANSI colored string (bytes)\nansi_text_bytes = b'\\x1b[31mHello, \\x1b[32mWorld!\\x1b[0m This is \\x1b[4mcolored\\x1b[0m text.'\n\n# Convert to plain text\nplain_text = ansi2txt(ansi_text_bytes)\n\nprint(f\"Original ANSI: {ansi_text_bytes.decode('utf-8')}\")\nprint(f\"Plain text: {plain_text}\")\n\n# Example with unicode string (needs encoding first)\nansi_text_str = '\\u001b[33mWarning:\\u001b[0m Something happened.'\nplain_text_from_str = ansi2txt(ansi_text_str.encode('utf-8'))\nprint(f\"Plain text from string: {plain_text_from_str}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your input is a `bytes` object. If you have a unicode string, encode it first: `ansi2txt(your_string.encode('utf-8'))`.","message":"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.","severity":"gotcha","affected_versions":"All versions (0.1.0+)"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions (0.1.0+)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install ansi2txt` to install the library.","cause":"The `ansi2txt` package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'ansi2txt'"},{"fix":"Convert your string to bytes before passing it to the function, e.g., `ansi2txt(my_string.encode('utf-8'))`.","cause":"You are passing a Python unicode string (`str`) directly to `ansi2txt` when it expects a byte string (`bytes`).","error":"TypeError: argument should be byte string or ASCII string, not str"}]}