Registry / serialization / textwrap3

textwrap3

JSON →
library0.9.2pypypi✓ verified 24d ago

textwrap3 is a compatibility back-port of Python 3.6's built-in `textwrap` module, providing its features (like `shorten` and `max_lines`) to older Python versions from 2.6 forward. It also includes minor tweaks, particularly around Unicode handling and em-dashes. The current version is 0.9.2, released in January 2019, indicating a maintenance-focused release cadence.

pip install textwrap3
INSTALL
IMPORT
SIG · TEXTWRAP3
T
textwrap3
serializationpythonv0.9.2
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.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.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

wrap
from textwrap3 import wrap
Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.
fill
from textwrap3 import fill
Import functions directly from 'textwrap3' as you would from the standard 'textwrap' module.
shorten
from textwrap3 import shorten
This function, backported from Python 3.4+, is available directly from 'textwrap3'.
TextWrapper
from textwrap3 import TextWrapper
The TextWrapper class for advanced configuration is imported directly.

Demonstrates basic text wrapping, filling, and shortening using the core functions from the textwrap3 library.

from textwrap3 import wrap, fill, shorten long_text = """This is a very long string that needs to be wrapped into several lines to be more readable. It demonstrates the basic functionality of the textwrap3 library, which is a backport of Python 3.6's textwrap module.""" # Using wrap to get a list of lines wrapped_lines = wrap(long_text, width=40) print("Wrapped lines (list):") for line in wrapped_lines: print(line) print("\n" + "-" * 30 + "\n") # Using fill to get a single string with newlines filled_text = fill(long_text, width=40) print("Filled text (single string):") print(filled_text) print("\n" + "-" * 30 + "\n") # Using shorten to truncate text shortened_text = shorten(long_text, width=50, placeholder=' [...]') print("Shortened text:") print(shortened_text)
Debug
Known issues
gotchaDue to its design principle, `textwrap3` adheres to Python 3's sensibilities, especially concerning Unicode. It uses `re.UNICODE` and treats Unicode em-dashes like ASCII '--'. This can lead to subtle differences in wrapping behavior compared to the native `textwrap` module in older Python 2.x environments where Unicode handling might differ.
fix
Be aware of potential Unicode handling discrepancies. Test text wrapping behavior thoroughly with diverse character sets, especially when migrating from native Python 2.x `textwrap`.
affects: All versions of textwrap3, specifically when used in Python 2.x environments.
gotchaThe `wrap()` and `fill()` functions, by default, replace all internal whitespace characters (including newlines and tabs) with single spaces before wrapping. Existing newlines in multi-paragraph input will be collapsed. If you need to preserve original newlines as paragraph breaks, process paragraphs separately or use `replace_whitespace=False` (though this might lead to other formatting challenges).
fix
Pass `replace_whitespace=False` to `wrap()` or `TextWrapper` constructor if you want to prevent the collapsing of whitespace. For multi-paragraph text, it's often better to split the text into paragraphs, wrap each, and then join them back.
affects: All versions of textwrap3.
gotchaFor modern Python 3.6+ environments, `textwrap3` is generally unnecessary. All the features it backports are already present in the standard library's `textwrap` module. Installing it in these environments adds an unnecessary dependency.
fix
For Python 3.6 and later, use the built-in `textwrap` module directly (e.g., `import textwrap`) instead of `textwrap3`. The functionality is identical.
affects: Users on Python 3.6 and newer.
gotchaThe underlying `textwrap.wrap()` logic (inherited by `textwrap3`) can exhibit poor performance (quadratic time complexity) when wrapping text containing extremely long, unbroken words (e.g., a very long string without spaces or hyphens) if `break_long_words` is `True` (the default).
fix
If you encounter performance issues with very long words, consider setting `break_on_hyphens=False` or `break_long_words=False` in the `TextWrapper` constructor or `wrap`/`fill` function calls if breaking long words is not critical, or pre-processing the text to insert break points.
affects: All versions of textwrap3.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'textwrap3'
The 'textwrap3' package is not installed in the current Python environment.
fix
pip install textwrap3
AttributeError: module 'textwrap' has no attribute 'shorten'
This error occurs on Python versions older than 3.4 when trying to use `textwrap.shorten` directly. Even if `textwrap3` is installed, you must explicitly import `textwrap3` as it does not automatically patch the built-in `textwrap` module.
fix
Instead of `import textwrap`, use `import textwrap3 as textwrap` or `from textwrap3 import shorten` to access the backported functionality:

```python
import textwrap3 as textwrap
# or from textwrap3 import shorten

my_string = "This is a very long string that needs to be shortened."
shortened_string = textwrap.shorten(my_string, width=20, placeholder="...")
print(shortened_string)
```
TypeError: shorten() got an unexpected keyword argument 'fix_sentence_endings'
The `fix_sentence_endings` argument is valid for `textwrap.wrap` and `textwrap.fill` but not for `textwrap.shorten` (or `textwrap3.shorten`).
fix
Remove the `fix_sentence_endings` argument, as it is not applicable to the `shorten` function:

```python
import textwrap3

text = "This is a sample sentence. It might be long."
# Incorrect:
# textwrap3.shorten(text, width=20, fix_sentence_endings=True)

# Correct:
shortened_text = textwrap3.shorten(text, width=20, placeholder="...")
print(shortened_text)
```
TypeError: shorten() missing 1 required positional argument: 'text'
The `textwrap3.shorten` function requires at least one positional argument: the string (`text`) to be shortened.
fix
Provide the `text` string as the first argument to the `shorten` function:

```python
import textwrap3

# Incorrect:
# textwrap3.shorten(width=10)

# Correct:
shortened_text = textwrap3.shorten("Hello, world! This is some text.", width=10)
print(shortened_text)
```
Upgrade
Version history
0.9.2latest on PyPI · released Jan 23, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources