JustifyText
*for monospaced fonts
Takes a string and inserts
spaces between words, until
line is desired width, like
you see here.
Importing the module and using the function:
- Option 1:
from justifytext import justify
justify(text, width)
- Option 2:
import justify
justifytext.justify(text, width)
Module Requirements/Dependencies:
- Python code tab = 4 spaces
- Requires textwrap module
Module Assumptions:
- Line has at least one space between words (at least in this version).
- Relies on break_on_long words equalling true in textwrapper. (You could make the change to the textwrapper function call in this module, but at a small enough width you would just get back lines with single words and nothing but spaces on the ends of them, i.e not justified.)
Required positional arguments:
- text == string to be formatted as list of strings exactly n characters long (You'll have to handle quotes and special characters of course.)
- width == desired width of the resulting line
Optional and default arguments:
justify_last_line = False
This is the default for handling the final line of the string. Convention is to NOT stretch a few words across the span of the formatted block, but I've included a way to undo that - if that's your kind of thing. For example:
text = "The end of this bit of text will be these three words."
justify(text, 35)
will return a list that can iterate over to display:
The end of this bit of text will be
these three words.
whereas
justify(text, 35, justify_last_line = True)
will return a iterable list that will display:
The end of this bit of text will be
these three words.
Module Flow:
-
Accepts a string (Module seems to ignore \n, not sure why yet -- I'm very tired. Probably has something to do with textwrap behavior. To preserve a special character, escape it, i.e. '\n'). Otherwise normal string manipulation rules apply, you'll need to handle quotation marks and special characters accordingly. One of the earliest steps in this module puts text through textwrapper, so if your input doesn't raise a textwrapper error there, you should be all right. (Probably).
-
Break into lines no longer than n using textwrap and TextWrapper. See discussion at end of 1. above.
-
Loop, starting from the last space (actually, the word before the last space), and walk backward, adding a space between words (on the theory that readers will be less likely to notice extra spaces when reading from left-to-right and encountering them at the end of a line.
-
If a line is still short after adding a space between all words in a line, module will start again at the last space (IOW, it enters a second or third or eighth space - beginning at the end - to reach desired width).
-
Once all lines are exactly n width, return list of lines. Module could just print them into a nice block, but I prefer to hand the reformatted text back to you, so you can do with it what you like. To display the reformatted text a simple iterable will do. e.g.:
for i in returnText:
print(i)
Sample Usage
Raw text:
text = "Now did the Lord say, \"First thou pullest the Holy Pin. Then thou must count to three.
Three shall be the number of the counting and the number of the counting shall be three. Four
shalt thou not count, neither shalt thou count two, excepting that thou then proceedeth to three.
Five is right out. Once the number three, being the number of the counting, be reached, then lobbest
thou the Holy Hand Grenade in the direction of thine foe, who, being naughty in my sight, shall snuff it."
Call:
x = justify(text, 30)
Returns:
x = ['Now did the Lord say, "First', 'thou pullest the Holy Pin.', 'Then thou must count to three.',
'Three shall be the number of', 'the counting and the number of', 'the counting shall be three.',
'Four shalt thou not count,', 'neither shalt thou count two,', 'excepting that thou then', 'proceedeth
to three. Five is', 'right out. Once the number', 'three, being the number of the', 'counting, be
reached, then', 'lobbest thou the Holy Hand', 'Grenade in the direction of', 'thine foe, who, being
naughty', 'in my sight, shall snuff it.']
Process list for justified display:
for i in x:
print(i)
Sample output:
Now did the Lord say, "First
thou pullest the Holy Pin.
Then thou must count to three.
Three shall be the number of
the counting and the number of
the counting shall be three.
Four shalt thou not count,
neither shalt thou count two,
excepting that thou then
proceedeth to three. Five is
right out. Once the number
three, being the number of the
counting, be reached, then
lobbest thou the Holy Hand
Grenade in the direction of
thine foe, who, being naughty
in my sight, shall snuff it.
Other/Misc.:
This package was prepared per the advice at: https://packaging.python.org
Found bugs? Let me know! mailto: cjtx.code@gmail.com
-cjtx / May 27, 2019
Metadata
Release files for JustifyText 0.2.1.post3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| JustifyText-0.2.1.post3.tar.gz | 4.5 kB | Without provenance | Details for JustifyText-0.2.1.post3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| JustifyText-0.2.1.post3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for JustifyText-0.2.1.post3-py2.py3-none-any.whl |
Total release size: 9.6 kB
Release files / JustifyText-0.2.1.post3.tar.gz
| Download URL | JustifyText-0.2.1.post3.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cedd8cae2d5e832399560aa7a252efc3fc1fc591ce271b54685026c2ee0483ea
|
|
BLAKE2b-256 checksum How to use checksums |
07852d4f064d6732fb4551fd434c6aacf0bc1b336e156c36394b77cfe9d9c206
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Release files / JustifyText-0.2.1.post3-py2.py3-none-any.whl
| Download URL | JustifyText-0.2.1.post3-py2.py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
1785e6845dc600c8cb38d1ffcb9d747e78d6ca453605af4cb30688842920fd95
|
|
BLAKE2b-256 checksum How to use checksums |
4f348c35cde4be22815cade6611141afe0b2dca8ade8de2149bfb7981d647ced
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Security for JustifyText 0.2.1.post3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.