Registry / serialization / ansi2html

ansi2html

JSON →
library1.9.2pypypi✓ verified 85d ago

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.

pip install ansi2html
INSTALL
IMPORT
SIG · ANSI2HTML
A
ansi2html
serializationpythonv1.9.2
Install
1.6s avg
Import
100ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.9.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.107s · 17.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.093s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Ansi2HTMLConverter
from ansi2html import Ansi2HTMLConverter

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], "...")
ansi2html --version
Debug
Known issues
gotchaThe `convert()` method's `full` parameter defaults to `True`, which generates a complete HTML document including `<html>`, `<head>`, `<body>` tags and embedded CSS. If you only need an HTML fragment to embed within an existing page, you must explicitly set `full=False`.
fix
Always specify `full=False` (e.g., `converter.convert(ansi_text, full=False)`) if you intend to embed the output into another HTML structure. Only use `full=True` if you're generating a standalone HTML file.
affects: All versions
gotchaWhen `full=False` is used, the generated HTML output contains CSS class names (e.g., `ansi-red`, `ansi-bold`) but no inline styles or `<style>` block. If you display this fragment without a corresponding stylesheet, the colors will not render.
fix
For `full=False` output, ensure you provide your own CSS to define the `ansi-*` classes. You can copy the default styles from the library's source or create custom ones. For `full=True`, styles are automatically included.
affects: All versions
Errors
Common errors & fixes
NameError: name 'Ansi2HTMLConverter' is not defined
The `Ansi2HTMLConverter` class was used without being correctly imported from the `ansi2html` library.
fix
Add the import statement `from ansi2html import Ansi2HTMLConverter` at the top of your Python file.
AttributeError: module 'ansi2html' has no attribute 'Ansi2HTMLConverter'
This usually happens when you try to access the class via `ansi2html.Ansi2HTMLConverter` after doing `import ansi2html`. The class is directly exported, not nested under the module object when using `from ansi2html import ...`. It can also occur if your script file is named `ansi2html.py`, causing a module shadowing issue.
fix
Use the direct import `from ansi2html import Ansi2HTMLConverter`. If your script file is named `ansi2html.py`, rename it to something else (e.g., `my_script.py`) to avoid conflicting with the library's module name.
Upgrade
Version history
1.9.2latest on PyPI · released Jun 22, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
ansi2html — pip install ansi2html · libregistry