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.'
Metadata
Release files for smartwrap 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| smartwrap-0.0.2.tar.gz | 2.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| smartwrap-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 5.4 kB
Release files / smartwrap-0.0.2.tar.gz
| Download URL | smartwrap-0.0.2.tar.gz |
|---|---|
| Size | 2.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8398e5add3016a00e5c9d6bf0c4998f1b115aa51454e5b5c8c270f130d14172
|
|
BLAKE2b-256 checksum How to use checksums |
bd293c52bfa72a9d131100354903053be983850bb2d4c8b37a3ecd5348658b8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / smartwrap-0.0.2-py3-none-any.whl
| Download URL | smartwrap-0.0.2-py3-none-any.whl |
|---|---|
| Size | 2.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4c97b360012e8270bbfbd6df870e3dac54274238d06c9a6ebbb9dc10f6761a63
|
|
BLAKE2b-256 checksum How to use checksums |
57c2dc57c4d8491adf122c3ffaa1337680996c9cceac327a85d639beef9bffb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|