Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`textwrap` --- Text wrapping and filling |
| 2 | ============================================= |
| 3 | |
| 4 | .. module:: textwrap |
| 5 | :synopsis: Text wrapping and filling |
| 6 | .. moduleauthor:: Greg Ward <gward@python.net> |
| 7 | .. sectionauthor:: Greg Ward <gward@python.net> |
| 8 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 9 | .. versionadded:: 2.3 |
| 10 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 11 | **Source code:** :source:`Lib/textwrap.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and |
| 16 | :func:`fill`, as well as :class:`TextWrapper`, the class that does all the work, |
| 17 | and a utility function :func:`dedent`. If you're just wrapping or filling one |
| 18 | or two text strings, the convenience functions should be good enough; |
| 19 | otherwise, you should use an instance of :class:`TextWrapper` for efficiency. |
| 20 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 21 | .. function:: wrap(text[, width[, ...]]) |
| 22 | |
| 23 | Wraps the single paragraph in *text* (a string) so every line is at most *width* |
| 24 | characters long. Returns a list of output lines, without final newlines. |
| 25 | |
| 26 | Optional keyword arguments correspond to the instance attributes of |
| 27 | :class:`TextWrapper`, documented below. *width* defaults to ``70``. |
| 28 | |
| 29 | |
| 30 | .. function:: fill(text[, width[, ...]]) |
| 31 | |
| 32 | Wraps the single paragraph in *text*, and returns a single string containing the |
| 33 | wrapped paragraph. :func:`fill` is shorthand for :: |
| 34 | |
| 35 | "\n".join(wrap(text, ...)) |
| 36 | |
| 37 | In particular, :func:`fill` accepts exactly the same keyword arguments as |
| 38 | :func:`wrap`. |
| 39 | |
| 40 | Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper` |
| 41 | instance and calling a single method on it. That instance is not reused, so for |
| 42 | applications that wrap/fill many text strings, it will be more efficient for you |
| 43 | to create your own :class:`TextWrapper` object. |
| 44 | |
Georg Brandl | 6f95ae5 | 2008-05-11 10:42:28 +0000 | [diff] [blame] | 45 | Text is preferably wrapped on whitespaces and right after the hyphens in |
| 46 | hyphenated words; only then will long words be broken if necessary, unless |
| 47 | :attr:`TextWrapper.break_long_words` is set to false. |
| 48 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 49 | An additional utility function, :func:`dedent`, is provided to remove |
| 50 | indentation from strings that have unwanted whitespace to the left of the text. |
| 51 | |
| 52 | |
| 53 | .. function:: dedent(text) |
| 54 | |
| 55 | Remove any common leading whitespace from every line in *text*. |
| 56 | |
| 57 | This can be used to make triple-quoted strings line up with the left edge of the |
| 58 | display, while still presenting them in the source code in indented form. |
| 59 | |
| 60 | Note that tabs and spaces are both treated as whitespace, but they are not |
| 61 | equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no |
| 62 | common leading whitespace. (This behaviour is new in Python 2.5; older versions |
| 63 | of this module incorrectly expanded tabs before searching for common leading |
| 64 | whitespace.) |
| 65 | |
| 66 | For example:: |
| 67 | |
| 68 | def test(): |
| 69 | # end first line with \ to avoid the empty line! |
| 70 | s = '''\ |
| 71 | hello |
| 72 | world |
| 73 | ''' |
| 74 | print repr(s) # prints ' hello\n world\n ' |
| 75 | print repr(dedent(s)) # prints 'hello\n world\n' |
| 76 | |
| 77 | |
| 78 | .. class:: TextWrapper(...) |
| 79 | |
| 80 | The :class:`TextWrapper` constructor accepts a number of optional keyword |
| 81 | arguments. Each argument corresponds to one instance attribute, so for example |
| 82 | :: |
| 83 | |
| 84 | wrapper = TextWrapper(initial_indent="* ") |
| 85 | |
| 86 | is the same as :: |
| 87 | |
| 88 | wrapper = TextWrapper() |
| 89 | wrapper.initial_indent = "* " |
| 90 | |
| 91 | You can re-use the same :class:`TextWrapper` object many times, and you can |
| 92 | change any of its options through direct assignment to instance attributes |
| 93 | between uses. |
| 94 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 95 | The :class:`TextWrapper` instance attributes (and keyword arguments to the |
| 96 | constructor) are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 97 | |
| 98 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 99 | .. attribute:: width |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 101 | (default: ``70``) The maximum length of wrapped lines. As long as there |
| 102 | are no individual words in the input text longer than :attr:`width`, |
| 103 | :class:`TextWrapper` guarantees that no output line will be longer than |
| 104 | :attr:`width` characters. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 105 | |
| 106 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 107 | .. attribute:: expand_tabs |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 109 | (default: ``True``) If true, then all tab characters in *text* will be |
| 110 | expanded to spaces using the :meth:`expandtabs` method of *text*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 111 | |
| 112 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 113 | .. attribute:: replace_whitespace |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 115 | (default: ``True``) If true, each whitespace character (as defined by |
| 116 | ``string.whitespace``) remaining after tab expansion will be replaced by a |
| 117 | single space. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 119 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 121 | If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, |
| 122 | each tab character will be replaced by a single space, which is *not* |
| 123 | the same as tab expansion. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 124 | |
Terry Reedy | 61ff85e | 2010-11-23 20:28:34 +0000 | [diff] [blame] | 125 | .. note:: |
| 126 | |
| 127 | If :attr:`replace_whitespace` is false, newlines may appear in the |
| 128 | middle of a line and cause strange output. For this reason, text should |
| 129 | be split into paragraphs (using :meth:`str.splitlines` or similar) |
| 130 | which are wrapped separately. |
| 131 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 133 | .. attribute:: drop_whitespace |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 135 | (default: ``True``) If true, whitespace that, after wrapping, happens to |
| 136 | end up at the beginning or end of a line is dropped (leading whitespace in |
| 137 | the first line is always preserved, though). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 138 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 139 | .. versionadded:: 2.6 |
| 140 | Whitespace was always dropped in earlier versions. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 141 | |
| 142 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 143 | .. attribute:: initial_indent |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 144 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 145 | (default: ``''``) String that will be prepended to the first line of |
| 146 | wrapped output. Counts towards the length of the first line. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 147 | |
| 148 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 149 | .. attribute:: subsequent_indent |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 151 | (default: ``''``) String that will be prepended to all lines of wrapped |
| 152 | output except the first. Counts towards the length of each line except |
| 153 | the first. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 154 | |
| 155 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 156 | .. attribute:: fix_sentence_endings |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 157 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 158 | (default: ``False``) If true, :class:`TextWrapper` attempts to detect |
| 159 | sentence endings and ensure that sentences are always separated by exactly |
| 160 | two spaces. This is generally desired for text in a monospaced font. |
| 161 | However, the sentence detection algorithm is imperfect: it assumes that a |
| 162 | sentence ending consists of a lowercase letter followed by one of ``'.'``, |
| 163 | ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, |
| 164 | followed by a space. One problem with this is algorithm is that it is |
| 165 | unable to detect the difference between "Dr." in :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 166 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 167 | [...] Dr. Frankenstein's monster [...] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 169 | and "Spot." in :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 170 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 171 | [...] See Spot. See Spot run [...] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 172 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 173 | :attr:`fix_sentence_endings` is false by default. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 174 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 175 | Since the sentence detection algorithm relies on ``string.lowercase`` for |
| 176 | the definition of "lowercase letter," and a convention of using two spaces |
| 177 | after a period to separate sentences on the same line, it is specific to |
| 178 | English-language texts. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 179 | |
| 180 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 181 | .. attribute:: break_long_words |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 182 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 183 | (default: ``True``) If true, then words longer than :attr:`width` will be |
| 184 | broken in order to ensure that no lines are longer than :attr:`width`. If |
| 185 | it is false, long words will not be broken, and some lines may be longer |
| 186 | than :attr:`width`. (Long words will be put on a line by themselves, in |
| 187 | order to minimize the amount by which :attr:`width` is exceeded.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 188 | |
Georg Brandl | 6f95ae5 | 2008-05-11 10:42:28 +0000 | [diff] [blame] | 189 | |
| 190 | .. attribute:: break_on_hyphens |
| 191 | |
| 192 | (default: ``True``) If true, wrapping will occur preferably on whitespaces |
| 193 | and right after hyphens in compound words, as it is customary in English. |
| 194 | If false, only whitespaces will be considered as potentially good places |
| 195 | for line breaks, but you need to set :attr:`break_long_words` to false if |
| 196 | you want truly insecable words. Default behaviour in previous versions |
| 197 | was to always allow breaking hyphenated words. |
| 198 | |
| 199 | .. versionadded:: 2.6 |
| 200 | |
| 201 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 202 | :class:`TextWrapper` also provides two public methods, analogous to the |
| 203 | module-level convenience functions: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 205 | .. method:: wrap(text) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 206 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 207 | Wraps the single paragraph in *text* (a string) so every line is at most |
| 208 | :attr:`width` characters long. All wrapping options are taken from |
| 209 | instance attributes of the :class:`TextWrapper` instance. Returns a list |
| 210 | of output lines, without final newlines. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 211 | |
| 212 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 213 | .. method:: fill(text) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 215 | Wraps the single paragraph in *text*, and returns a single string |
| 216 | containing the wrapped paragraph. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 217 | |