ansiwrap
textwrap, but savvy to ANSI colors and styles
What it is and what it does
ansiwrap extends Python's standard textwrap module to handle ANSI escape sequences correctly. When you wrap or fill text containing ANSI color codes, textwrap miscalculates line length because it counts the raw bytes of invisible control codes. ansiwrap strips those codes during measurement, so wrapped lines respect actual display width, not byte length.
The package provides wrap(), fill(), and shorten() functions mirroring textwrap's API, plus utility functions like ansilen() to measure visible text length and strip_color() to remove codes entirely. It has no runtime dependencies and works across Python 2 and 3, making it straightforward to drop into terminal UI code or CLI tools that use colored output.
Use it for:
- Format colored log output to fit terminal width without breaking lines due to invisible codes
- Wrap help text in CLI tools that use ANSI colors for emphasis without miscounting line length
- Truncate colored status messages to a fixed display width in terminal dashboards
- Process multi-line colored text and reflow it to new widths while preserving styling
- Strip ANSI codes from colored strings when you need plain text for logging or storage
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Wraps and formats text while correctly handling ANSI color and style codes, treating them as invisible to line length calculations the way textwrap cannot.
Yes. The package solves a real problem (textwrap + ANSI codes) with no dependencies, permissive licensing, and active maintenance. It's small, focused, and widely downloaded. Install it if you're building terminal UIs or CLI tools with colored output that need proper text wrapping.
Install
ansiwrap on PyPI
pip
pip install ansiwrapuv
uv add ansiwrappoetry
poetry add ansiwrapInstalling ansiwrap
Before you install
Low friction: pure Python wheel with no runtime dependencies. Maintenance is active with recent commits, though the latest release was in 2019.
License in practice
Apache License 2.0 is permissive; you can use this in commercial and proprietary projects with minimal restriction.
Quickstart
pip install ansiwrap
from ansiwrap import wrap, fill, shorten, ansilen, strip_color
lines = wrap('colored text', width=20)
filled = fill('colored text', width=20)
shortened = shorten('colored text', 20, placeholder='...')
Verify before relying
- Whether the package handles all ANSI escape sequences or only color/style codes
- Performance characteristics on very long strings or high-volume wrapping operations
- Exact Python version support beyond the classifiers listed (2.6, 2.7, 3.3-3.7)
Package facts
| License | Apache License 2.0 (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 2,760 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,218,912/month — #4,202 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: ansiwrap-0.8.4-py2.py3-none-any.whl
Keywords: text, textwrap, ANSI, colors
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
ansicolorsEmbeds ANSI color and style codes into Python…
permissive · top 5,000 on PyPI
textwrap3Backports Python 3.6's textwrap module to…
permissive · top 5,000 on PyPI
coloredColored provides ANSI/VT100 escape sequences…
permissive · top 5,000 on PyPI
crayonsCrayons provides colored text output for…
permissive · top 15,000 on PyPI
termcolorAdds ANSI color and text formatting (bold,…
permissive · top 1,000 on PyPI
strip-ansiRemoves ANSI escape sequences (color codes,…
unclear · top 15,000 on PyPI
ansiProvides ANSI escape codes for cursor movement…
permissive · top 15,000 on PyPI
colorfulColorful applies ANSI color and text styling to…
permissive · top 5,000 on PyPI
enrichExtends the rich library with a Console class…
permissive · top 5,000 on PyPI
treetableRenders nested data structures as formatted…
permissive · top 15,000 on PyPI