ansi2html

1.9.2 · active · verified Fri Apr 17

ansi2html is a Python library that converts text containing ANSI escape codes (commonly used for colored terminal output) into HTML or LaTeX markup. It is currently at version 1.9.2 and maintains an active development cycle with regular updates addressing features and minor bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to convert a string with ANSI color codes into an HTML fragment for embedding, or a complete HTML document with styling.

from ansi2html import Ansi2HTMLConverter
import os

# Example ANSI text with colors (red, yellow, green)
ansi_text = "\x1b[31mError:\x1b[0m \x1b[33mWarning:\x1b[0m \x1b[32mSuccess!\x1b[0m"

# Create a converter instance
converter = Ansi2HTMLConverter()

# Convert ANSI text to an HTML fragment (recommended for embedding)
html_fragment = converter.convert(ansi_text, full=False)
print("HTML Fragment:\n", html_fragment)

# To generate a full HTML document with embedded CSS
html_full_doc = converter.convert(ansi_text, full=True, title="ANSI Log Output")
print("\nFull HTML Document (first 200 chars):\n", html_full_doc[:200], "...")

view raw JSON →