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