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