A smart text wrapper
Project description
smartwrap is an extension of the standard library textwrap which enables balanced text wrapping.
Wrapped text is balanced when the number of characters across all rows is minimized, while keeping the number of rows identical to that of unbalanced wrapping. Note that this is numerical balancing – not optical balancing, which takes more factors into consideration such as kerning and letter size.
Usage
Example
UDHR = """All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood."""
$ textwrap.wrap(UDHR)
['All human beings are born free and equal in dignity and rights. They',
'are endowed with reason and conscience and should act towards one',
'another in a spirit of brotherhood.']
$ smartwrap.wrap(UDHR, balanced=True)
['All human beings are born free and equal in dignity and',
'rights. They are endowed with reason and conscience and',
'should act towards one another in a spirit of brotherhood.']
Hint
smartwrap is most useful when wrapping short pieces of text which could flow into multiple rows depending on the container size. To prevent awkward splitting of the text into one long row and one short row, smartwrap balances the number of letters between the rows. Some cases include labels on legends and buttons, quoted statements, etc.
# Example: a pie chart has a label 'Social and cultural rights'; labels cannot be longer than 20 characters.
$ textwrap.wrap("Social and cultural rights", width=20)
['Social and cultural', 'rights']
$ smartwrap.wrap("Social and cultural rights", balanced=True, width=20)
['Social and', 'cultural rights']
Note that smartwrap can be used like textwrap. It returns the same outputs when balanced flag is set to False. Also it accepts the same arguments and has the same methods.
$ smartwrap.wrap(UDHR) # no difference to `textwrap`
['All human beings are born free and equal in dignity and rights. They',
'are endowed with reason and conscience and should act towards one',
'another in a spirit of brotherhood.']
$ smartwrap.wrap(UDHR, balanced=True, width=50) # width arg and other args available
['All human beings are born free and equal in',
'dignity and rights. They are endowed with',
'reason and conscience and should act towards',
'one another in a spirit of brotherhood.']
$ smartwrap.fill(UDHR, balanced=True, width=50) # .fill() and other methods available
'All human beings are born free and equal in\ndignity and rights. They are endowed with\nreason and conscience and should act towards\none another in a spirit of brotherhood.'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smartwrap-0.0.2.tar.gz.
File metadata
- Download URL: smartwrap-0.0.2.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8398e5add3016a00e5c9d6bf0c4998f1b115aa51454e5b5c8c270f130d14172
|
|
| MD5 |
f41d37d4d960ea6198559af5be513251
|
|
| BLAKE2b-256 |
bd293c52bfa72a9d131100354903053be983850bb2d4c8b37a3ecd5348658b8f
|
File details
Details for the file smartwrap-0.0.2-py3-none-any.whl.
File metadata
- Download URL: smartwrap-0.0.2-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c97b360012e8270bbfbd6df870e3dac54274238d06c9a6ebbb9dc10f6761a63
|
|
| MD5 |
94ac3555ce6e47cbc68ad2f01fa97384
|
|
| BLAKE2b-256 |
57c2dc57c4d8491adf122c3ffaa1337680996c9cceac327a85d639beef9bffb7
|