Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/textwrap.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 11 | -------------- |
| 12 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 13 | The :mod:`textwrap` module provides some convenience functions, |
| 14 | as well as :class:`TextWrapper`, the class that does all the work. |
| 15 | If you're just wrapping or filling one or two text strings, the convenience |
| 16 | functions should be good enough; otherwise, you should use an instance of |
| 17 | :class:`TextWrapper` for efficiency. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 19 | .. function:: wrap(text, width=70, **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 21 | Wraps the single paragraph in *text* (a string) so every line is at most |
| 22 | *width* characters long. Returns a list of output lines, without final |
| 23 | newlines. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | Optional keyword arguments correspond to the instance attributes of |
| 26 | :class:`TextWrapper`, documented below. *width* defaults to ``70``. |
| 27 | |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 28 | See the :meth:`TextWrapper.wrap` method for additional details on how |
| 29 | :func:`wrap` behaves. |
| 30 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 32 | .. function:: fill(text, width=70, **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | Wraps the single paragraph in *text*, and returns a single string containing the |
| 35 | wrapped paragraph. :func:`fill` is shorthand for :: |
| 36 | |
| 37 | "\n".join(wrap(text, ...)) |
| 38 | |
| 39 | In particular, :func:`fill` accepts exactly the same keyword arguments as |
| 40 | :func:`wrap`. |
| 41 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 43 | .. function:: shorten(text, width, **kwargs) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 44 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 45 | Collapse and truncate the given *text* to fit in the given *width*. |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 46 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 47 | First the whitespace in *text* is collapsed (all whitespace is replaced by |
| 48 | single spaces). If the result fits in the *width*, it is returned. |
| 49 | Otherwise, enough words are dropped from the end so that the remaining words |
| 50 | plus the :attr:`placeholder` fit within :attr:`width`:: |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 51 | |
| 52 | >>> textwrap.shorten("Hello world!", width=12) |
| 53 | 'Hello world!' |
| 54 | >>> textwrap.shorten("Hello world!", width=11) |
Antoine Pitrou | c593056 | 2013-08-16 22:31:12 +0200 | [diff] [blame] | 55 | 'Hello [...]' |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 56 | >>> textwrap.shorten("Hello world", width=10, placeholder="...") |
| 57 | 'Hello...' |
| 58 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 59 | Optional keyword arguments correspond to the instance attributes of |
| 60 | :class:`TextWrapper`, documented below. Note that the whitespace is |
| 61 | collapsed before the text is passed to the :class:`TextWrapper` :meth:`fill` |
| 62 | function, so changing the value of :attr:`.tabsize`, :attr:`.expand_tabs`, |
| 63 | :attr:`.drop_whitespace`, and :attr:`.replace_whitespace` will have no effect. |
| 64 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 65 | .. versionadded:: 3.4 |
| 66 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | .. function:: dedent(text) |
| 69 | |
| 70 | Remove any common leading whitespace from every line in *text*. |
| 71 | |
| 72 | This can be used to make triple-quoted strings line up with the left edge of the |
| 73 | display, while still presenting them in the source code in indented form. |
| 74 | |
| 75 | Note that tabs and spaces are both treated as whitespace, but they are not |
| 76 | equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 77 | common leading whitespace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | For example:: |
| 80 | |
| 81 | def test(): |
| 82 | # end first line with \ to avoid the empty line! |
| 83 | s = '''\ |
| 84 | hello |
| 85 | world |
| 86 | ''' |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 87 | print(repr(s)) # prints ' hello\n world\n ' |
| 88 | print(repr(dedent(s))) # prints 'hello\n world\n' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | |
| 90 | |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 91 | .. function:: indent(text, prefix, predicate=None) |
| 92 | |
| 93 | Add *prefix* to the beginning of selected lines in *text*. |
| 94 | |
| 95 | Lines are separated by calling ``text.splitlines(True)``. |
| 96 | |
| 97 | By default, *prefix* is added to all lines that do not consist |
| 98 | solely of whitespace (including any line endings). |
| 99 | |
| 100 | For example:: |
| 101 | |
| 102 | >>> s = 'hello\n\n \nworld' |
| 103 | >>> indent(s, ' ') |
| 104 | ' hello\n\n \n world' |
| 105 | |
| 106 | The optional *predicate* argument can be used to control which lines |
| 107 | are indented. For example, it is easy to add *prefix* to even empty |
| 108 | and whitespace-only lines:: |
| 109 | |
| 110 | >>> print(indent(s, '+ ', lambda line: True)) |
| 111 | + hello |
| 112 | + |
| 113 | + |
| 114 | + world |
| 115 | |
Raymond Hettinger | dc69e03 | 2014-11-05 21:27:56 -0800 | [diff] [blame] | 116 | .. versionadded:: 3.3 |
| 117 | |
Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 118 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 119 | :func:`wrap`, :func:`fill` and :func:`shorten` work by creating a |
| 120 | :class:`TextWrapper` instance and calling a single method on it. That |
| 121 | instance is not reused, so for applications that process many text |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 122 | strings using :func:`wrap` and/or :func:`fill`, it may be more efficient to |
| 123 | create your own :class:`TextWrapper` object. |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 124 | |
| 125 | Text is preferably wrapped on whitespaces and right after the hyphens in |
| 126 | hyphenated words; only then will long words be broken if necessary, unless |
| 127 | :attr:`TextWrapper.break_long_words` is set to false. |
| 128 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 129 | .. class:: TextWrapper(**kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
| 131 | The :class:`TextWrapper` constructor accepts a number of optional keyword |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 132 | arguments. Each keyword argument corresponds to an instance attribute, so |
| 133 | for example :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
| 135 | wrapper = TextWrapper(initial_indent="* ") |
| 136 | |
| 137 | is the same as :: |
| 138 | |
| 139 | wrapper = TextWrapper() |
| 140 | wrapper.initial_indent = "* " |
| 141 | |
| 142 | You can re-use the same :class:`TextWrapper` object many times, and you can |
| 143 | change any of its options through direct assignment to instance attributes |
| 144 | between uses. |
| 145 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 146 | The :class:`TextWrapper` instance attributes (and keyword arguments to the |
| 147 | constructor) are as follows: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
| 149 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 150 | .. attribute:: width |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 152 | (default: ``70``) The maximum length of wrapped lines. As long as there |
| 153 | are no individual words in the input text longer than :attr:`width`, |
| 154 | :class:`TextWrapper` guarantees that no output line will be longer than |
| 155 | :attr:`width` characters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
| 157 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 158 | .. attribute:: expand_tabs |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 160 | (default: ``True``) If true, then all tab characters in *text* will be |
| 161 | expanded to spaces using the :meth:`expandtabs` method of *text*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | |
Hynek Schlawack | d527259 | 2012-05-19 13:33:11 +0200 | [diff] [blame] | 164 | .. attribute:: tabsize |
| 165 | |
| 166 | (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters |
| 167 | in *text* will be expanded to zero or more spaces, depending on the |
| 168 | current column and the given tab size. |
| 169 | |
| 170 | .. versionadded:: 3.3 |
| 171 | |
| 172 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 173 | .. attribute:: replace_whitespace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
Andrew Svetlov | 59db401 | 2012-08-13 23:22:23 +0300 | [diff] [blame] | 175 | (default: ``True``) If true, after tab expansion but before wrapping, |
| 176 | the :meth:`wrap` method will replace each whitespace character |
| 177 | with a single space. The whitespace characters replaced are |
| 178 | as follows: tab, newline, vertical tab, formfeed, and carriage |
| 179 | return (``'\t\n\v\f\r'``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 181 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 183 | If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, |
| 184 | each tab character will be replaced by a single space, which is *not* |
| 185 | the same as tab expansion. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Terry Reedy | 6d2ab71 | 2010-11-23 20:17:24 +0000 | [diff] [blame] | 187 | .. note:: |
| 188 | |
| 189 | If :attr:`replace_whitespace` is false, newlines may appear in the |
| 190 | middle of a line and cause strange output. For this reason, text should |
| 191 | be split into paragraphs (using :meth:`str.splitlines` or similar) |
| 192 | which are wrapped separately. |
| 193 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 195 | .. attribute:: drop_whitespace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 197 | (default: ``True``) If true, whitespace at the beginning and ending of |
| 198 | every line (after wrapping but before indenting) is dropped. |
| 199 | Whitespace at the beginning of the paragraph, however, is not dropped |
| 200 | if non-whitespace follows it. If whitespace being dropped takes up an |
| 201 | entire line, the whole line is dropped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 204 | .. attribute:: initial_indent |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 206 | (default: ``''``) String that will be prepended to the first line of |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 207 | wrapped output. Counts towards the length of the first line. The empty |
| 208 | string is not indented. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
| 210 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 211 | .. attribute:: subsequent_indent |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 213 | (default: ``''``) String that will be prepended to all lines of wrapped |
| 214 | output except the first. Counts towards the length of each line except |
| 215 | the first. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | |
| 217 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 218 | .. attribute:: fix_sentence_endings |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 220 | (default: ``False``) If true, :class:`TextWrapper` attempts to detect |
| 221 | sentence endings and ensure that sentences are always separated by exactly |
| 222 | two spaces. This is generally desired for text in a monospaced font. |
| 223 | However, the sentence detection algorithm is imperfect: it assumes that a |
| 224 | sentence ending consists of a lowercase letter followed by one of ``'.'``, |
| 225 | ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, |
| 226 | followed by a space. One problem with this is algorithm is that it is |
| 227 | unable to detect the difference between "Dr." in :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 229 | [...] Dr. Frankenstein's monster [...] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 231 | and "Spot." in :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 233 | [...] See Spot. See Spot run [...] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 235 | :attr:`fix_sentence_endings` is false by default. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 237 | Since the sentence detection algorithm relies on ``string.lowercase`` for |
| 238 | the definition of "lowercase letter," and a convention of using two spaces |
| 239 | after a period to separate sentences on the same line, it is specific to |
| 240 | English-language texts. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
| 242 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 243 | .. attribute:: break_long_words |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 245 | (default: ``True``) If true, then words longer than :attr:`width` will be |
| 246 | broken in order to ensure that no lines are longer than :attr:`width`. If |
| 247 | it is false, long words will not be broken, and some lines may be longer |
| 248 | than :attr:`width`. (Long words will be put on a line by themselves, in |
| 249 | order to minimize the amount by which :attr:`width` is exceeded.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 251 | |
| 252 | .. attribute:: break_on_hyphens |
| 253 | |
| 254 | (default: ``True``) If true, wrapping will occur preferably on whitespaces |
| 255 | and right after hyphens in compound words, as it is customary in English. |
| 256 | If false, only whitespaces will be considered as potentially good places |
| 257 | for line breaks, but you need to set :attr:`break_long_words` to false if |
| 258 | you want truly insecable words. Default behaviour in previous versions |
| 259 | was to always allow breaking hyphenated words. |
| 260 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 261 | |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 262 | .. attribute:: max_lines |
| 263 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 264 | (default: ``None``) If not ``None``, then the output will contain at most |
| 265 | *max_lines* lines, with *placeholder* appearing at the end of the output. |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 266 | |
| 267 | .. versionadded:: 3.4 |
| 268 | |
| 269 | |
| 270 | .. attribute:: placeholder |
| 271 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 272 | (default: ``' [...]'``) String that will appear at the end of the output |
| 273 | text if it has been truncated. |
Serhiy Storchaka | acc9f3f | 2013-10-15 21:22:54 +0300 | [diff] [blame] | 274 | |
| 275 | .. versionadded:: 3.4 |
| 276 | |
| 277 | |
Antoine Pitrou | 389dec8 | 2013-08-12 22:39:09 +0200 | [diff] [blame] | 278 | :class:`TextWrapper` also provides some public methods, analogous to the |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 279 | module-level convenience functions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 281 | .. method:: wrap(text) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 283 | Wraps the single paragraph in *text* (a string) so every line is at most |
| 284 | :attr:`width` characters long. All wrapping options are taken from |
R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 285 | instance attributes of the :class:`TextWrapper` instance. Returns a list |
| 286 | of output lines, without final newlines. If the wrapped output has no |
| 287 | content, the returned list is empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | |
| 289 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 290 | .. method:: fill(text) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 292 | Wraps the single paragraph in *text*, and returns a single string |
| 293 | containing the wrapped paragraph. |