{"id":9462,"library":"ansi2html","title":"ansi2html","description":"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.","status":"active","version":"1.9.2","language":"en","source_language":"en","source_url":"https://github.com/manojmj92/ansi2html","tags":["ansi","html","converter","terminal","colors","latex","markup"],"install":[{"cmd":"pip install ansi2html","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Ansi2HTMLConverter","correct":"from ansi2html import Ansi2HTMLConverter"}],"quickstart":{"code":"from ansi2html import Ansi2HTMLConverter\nimport os\n\n# Example ANSI text with colors (red, yellow, green)\nansi_text = \"\\x1b[31mError:\\x1b[0m \\x1b[33mWarning:\\x1b[0m \\x1b[32mSuccess!\\x1b[0m\"\n\n# Create a converter instance\nconverter = Ansi2HTMLConverter()\n\n# Convert ANSI text to an HTML fragment (recommended for embedding)\nhtml_fragment = converter.convert(ansi_text, full=False)\nprint(\"HTML Fragment:\\n\", html_fragment)\n\n# To generate a full HTML document with embedded CSS\nhtml_full_doc = converter.convert(ansi_text, full=True, title=\"ANSI Log Output\")\nprint(\"\\nFull HTML Document (first 200 chars):\\n\", html_full_doc[:200], \"...\")","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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`.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"When `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Add the import statement `from ansi2html import Ansi2HTMLConverter` at the top of your Python file.","cause":"The `Ansi2HTMLConverter` class was used without being correctly imported from the `ansi2html` library.","error":"NameError: name 'Ansi2HTMLConverter' is not defined"},{"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.","cause":"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.","error":"AttributeError: module 'ansi2html' has no attribute 'Ansi2HTMLConverter'"}]}