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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
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 | |
| 28 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 29 | .. function:: fill(text, width=70, **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +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 | 116aa62 | 2007-08-15 14:28:22 +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 |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 61 | common leading whitespace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | For example:: |
| 64 | |
| 65 | def test(): |
| 66 | # end first line with \ to avoid the empty line! |
| 67 | s = '''\ |
| 68 | hello |
| 69 | world |
| 70 | ''' |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 71 | print(repr(s)) # prints ' hello\n world\n ' |
| 72 | print(repr(dedent(s))) # prints 'hello\n world\n' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 75 | .. class:: TextWrapper(**kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | The :class:`TextWrapper` constructor accepts a number of optional keyword |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 78 | arguments. Each keyword argument corresponds to an instance attribute, so |
| 79 | for example :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | wrapper = TextWrapper(initial_indent="* ") |
| 82 | |
| 83 | is the same as :: |
| 84 | |
| 85 | wrapper = TextWrapper() |
| 86 | wrapper.initial_indent = "* " |
| 87 | |
| 88 | You can re-use the same :class:`TextWrapper` object many times, and you can |
| 89 | change any of its options through direct assignment to instance attributes |
| 90 | between uses. |
| 91 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 92 | The :class:`TextWrapper` instance attributes (and keyword arguments to the |
| 93 | constructor) are as follows: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
| 95 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 96 | .. attribute:: width |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 98 | (default: ``70``) The maximum length of wrapped lines. As long as there |
| 99 | are no individual words in the input text longer than :attr:`width`, |
| 100 | :class:`TextWrapper` guarantees that no output line will be longer than |
| 101 | :attr:`width` characters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 104 | .. attribute:: expand_tabs |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 106 | (default: ``True``) If true, then all tab characters in *text* will be |
| 107 | expanded to spaces using the :meth:`expandtabs` method of *text*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
| 109 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 110 | .. attribute:: replace_whitespace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 112 | (default: ``True``) If true, each whitespace character (as defined by |
| 113 | ``string.whitespace``) remaining after tab expansion will be replaced by a |
| 114 | single space. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 116 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 118 | If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, |
| 119 | each tab character will be replaced by a single space, which is *not* |
| 120 | the same as tab expansion. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |
Terry Reedy | 6d2ab71 | 2010-11-23 20:17:24 +0000 | [diff] [blame] | 122 | .. note:: |
| 123 | |
| 124 | If :attr:`replace_whitespace` is false, newlines may appear in the |
| 125 | middle of a line and cause strange output. For this reason, text should |
| 126 | be split into paragraphs (using :meth:`str.splitlines` or similar) |
| 127 | which are wrapped separately. |
| 128 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 130 | .. attribute:: drop_whitespace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 132 | (default: ``True``) If true, whitespace that, after wrapping, happens to |
| 133 | end up at the beginning or end of a line is dropped (leading whitespace in |
| 134 | the first line is always preserved, though). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 137 | .. attribute:: initial_indent |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 139 | (default: ``''``) String that will be prepended to the first line of |
| 140 | wrapped output. Counts towards the length of the first line. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 143 | .. attribute:: subsequent_indent |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 145 | (default: ``''``) String that will be prepended to all lines of wrapped |
| 146 | output except the first. Counts towards the length of each line except |
| 147 | the first. |
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:: fix_sentence_endings |
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: ``False``) If true, :class:`TextWrapper` attempts to detect |
| 153 | sentence endings and ensure that sentences are always separated by exactly |
| 154 | two spaces. This is generally desired for text in a monospaced font. |
| 155 | However, the sentence detection algorithm is imperfect: it assumes that a |
| 156 | sentence ending consists of a lowercase letter followed by one of ``'.'``, |
| 157 | ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, |
| 158 | followed by a space. One problem with this is algorithm is that it is |
| 159 | unable to detect the difference between "Dr." in :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 161 | [...] Dr. Frankenstein's monster [...] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 163 | and "Spot." in :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 165 | [...] See Spot. See Spot run [...] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 167 | :attr:`fix_sentence_endings` is false by default. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 169 | Since the sentence detection algorithm relies on ``string.lowercase`` for |
| 170 | the definition of "lowercase letter," and a convention of using two spaces |
| 171 | after a period to separate sentences on the same line, it is specific to |
| 172 | English-language texts. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
| 174 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 175 | .. attribute:: break_long_words |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 177 | (default: ``True``) If true, then words longer than :attr:`width` will be |
| 178 | broken in order to ensure that no lines are longer than :attr:`width`. If |
| 179 | it is false, long words will not be broken, and some lines may be longer |
| 180 | than :attr:`width`. (Long words will be put on a line by themselves, in |
| 181 | order to minimize the amount by which :attr:`width` is exceeded.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 183 | |
| 184 | .. attribute:: break_on_hyphens |
| 185 | |
| 186 | (default: ``True``) If true, wrapping will occur preferably on whitespaces |
| 187 | and right after hyphens in compound words, as it is customary in English. |
| 188 | If false, only whitespaces will be considered as potentially good places |
| 189 | for line breaks, but you need to set :attr:`break_long_words` to false if |
| 190 | you want truly insecable words. Default behaviour in previous versions |
| 191 | was to always allow breaking hyphenated words. |
| 192 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 193 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 194 | :class:`TextWrapper` also provides two public methods, analogous to the |
| 195 | module-level convenience functions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 197 | .. method:: wrap(text) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +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 | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
| 204 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 205 | .. method:: fill(text) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 207 | Wraps the single paragraph in *text*, and returns a single string |
| 208 | containing the wrapped paragraph. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |