blob: 8c58d5dcd471ffd30aedb8b49cc544989b44f0fa [file] [log] [blame]
Greg Wardae64f3ad2002-06-29 02:38:50 +00001\section{\module{textwrap} ---
2 Text wrapping and filling}
3
4\declaremodule{standard}{textwrap}
5\modulesynopsis{Text wrapping and filling}
6\moduleauthor{Greg Ward}{gward@python.net}
7\sectionauthor{Greg Ward}{gward@python.net}
8
9\versionadded{2.3}
10
11The \module{textwrap} module provides two convenience functions,
12\function{wrap()} and \function{fill()}, as well as
13\class{TextWrapper}, the class that does all the work. If you're just
14wrapping or filling one or two text strings, the convenience functions
15should be good enough; otherwise, you should use an instance of
16\class{TextWrapper} for efficiency.
17
Fred Drakeca23ee22002-07-02 20:37:12 +000018\begin{funcdesc}{wrap}{text\optional{, width\optional{, \moreargs}}}
Greg Wardae64f3ad2002-06-29 02:38:50 +000019Wraps the single paragraph in \var{text} (a string) so every line is at
20most \var{width} characters long. Returns a list of output lines,
21without final newlines.
22
23Optional keyword arguments correspond to the instance attributes of
Fred Drakeca23ee22002-07-02 20:37:12 +000024\class{TextWrapper}, documented below. \var{width} defaults to
25\code{70}.
Greg Wardae64f3ad2002-06-29 02:38:50 +000026\end{funcdesc}
27
Fred Drakeca23ee22002-07-02 20:37:12 +000028\begin{funcdesc}{fill}{text\optional{, width\optional{, \moreargs}}}
Greg Wardae64f3ad2002-06-29 02:38:50 +000029Wraps the single paragraph in \var{text}, and returns a single string
30containing the wrapped paragraph. \function{fill()} is shorthand for
31\begin{verbatim}
32"\n".join(wrap(text, ...))
33\end{verbatim}
34
35In particular, \function{fill()} accepts exactly the same keyword
36arguments as \function{wrap()}.
37\end{funcdesc}
38
39Both \function{wrap()} and \function{fill()} work by creating a
40\class{TextWrapper} instance and calling a single method on it. That
41instance is not reused, so for applications that wrap/fill many text
42strings, it will be more efficient for you to create your own
43\class{TextWrapper} object.
44
Greg Ward285f4a72002-07-02 21:48:12 +000045\begin{classdesc}{TextWrapper}{...}
46The \class{TextWrapper} constructor accepts a number of optional
47keyword arguments. Each argument corresponds to one instance attribute,
48so for example
Greg Wardae64f3ad2002-06-29 02:38:50 +000049\begin{verbatim}
50wrapper = TextWrapper(initial_indent="* ")
51\end{verbatim}
52is the same as
53\begin{verbatim}
54wrapper = TextWrapper()
55wrapper.initial_indent = "* "
56\end{verbatim}
57
58You can re-use the same \class{TextWrapper} object many times, and you
59can change any of its options through direct assignment to instance
Fred Drakeca23ee22002-07-02 20:37:12 +000060attributes between uses.
61\end{classdesc}
Greg Wardae64f3ad2002-06-29 02:38:50 +000062
Greg Ward285f4a72002-07-02 21:48:12 +000063The \class{TextWrapper} instance attributes (and keyword arguments to
64the constructor) are as follows:
Fred Drakeca23ee22002-07-02 20:37:12 +000065
Greg Ward285f4a72002-07-02 21:48:12 +000066\begin{memberdesc}{width}
Fred Drake228f6e42002-07-03 05:08:48 +000067(default: \code{70}) The maximum length of wrapped lines. As long as
68there are no individual words in the input text longer than
69\member{width}, \class{TextWrapper} guarantees that no output line
70will be longer than \member{width} characters.
Greg Ward285f4a72002-07-02 21:48:12 +000071\end{memberdesc}
Fred Drakeca23ee22002-07-02 20:37:12 +000072
73\begin{memberdesc}{expand_tabs}
Greg Ward285f4a72002-07-02 21:48:12 +000074(default: \code{True}) If true, then all tab characters in \var{text}
75will be expanded to spaces using the \method{expand_tabs()} method of
Greg Wardae64f3ad2002-06-29 02:38:50 +000076\var{text}.
77\end{memberdesc}
78
Fred Drakeca23ee22002-07-02 20:37:12 +000079\begin{memberdesc}{replace_whitespace}
Fred Drake228f6e42002-07-03 05:08:48 +000080(default: \code{True}) If true, each whitespace character (as defined
81by \code{string.whitespace}) remaining after tab expansion will be
82replaced by a single space. \note{If \member{expand_tabs} is false
83and \member{replace_whitespace} is true, each tab character will be
84replaced by a single space, which is \emph{not} the same as tab
85expansion.}
Greg Wardae64f3ad2002-06-29 02:38:50 +000086\end{memberdesc}
87
Fred Drakeca23ee22002-07-02 20:37:12 +000088\begin{memberdesc}{initial_indent}
89(default: \code{''}) String that will be prepended to the first line
90of wrapped output. Counts towards the length of the first line.
Greg Wardae64f3ad2002-06-29 02:38:50 +000091\end{memberdesc}
92
Fred Drakeca23ee22002-07-02 20:37:12 +000093\begin{memberdesc}{subsequent_indent}
94(default: \code{''}) String that will be prepended to all lines of
95wrapped output except the first. Counts towards the length of each
96line except the first.
Greg Wardae64f3ad2002-06-29 02:38:50 +000097\end{memberdesc}
98
Fred Drakeca23ee22002-07-02 20:37:12 +000099\begin{memberdesc}{fix_sentence_endings}
100(default: \code{False}) If true, \class{TextWrapper} attempts to detect
Greg Wardae64f3ad2002-06-29 02:38:50 +0000101sentence endings and ensure that sentences are always separated by
102exactly two spaces. This is generally desired for text in a monospaced
103font. However, the sentence detection algorithm is imperfect: it
104assumes that a sentence ending consists of a lowercase letter followed
105by one of \character{.},
106\character{!}, or \character{?}, possibly followed by one of
Greg Ward285f4a72002-07-02 21:48:12 +0000107\character{"} or \character{'}, followed by a space. One problem
108with this is algorithm is that it is unable to detect the difference
109between ``Dr.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000110
Greg Wardae64f3ad2002-06-29 02:38:50 +0000111\begin{verbatim}
112[...] Dr. Frankenstein's monster [...]
113\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000114
Greg Wardae64f3ad2002-06-29 02:38:50 +0000115and ``Spot.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000116
Greg Wardae64f3ad2002-06-29 02:38:50 +0000117\begin{verbatim}
Greg Ward285f4a72002-07-02 21:48:12 +0000118[...] See Spot. See Spot run [...]
Greg Wardae64f3ad2002-06-29 02:38:50 +0000119\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000120
121\member{fix_sentence_endings} is false by default.
122
123Since the sentence detection algorithm relies on
124\code{string.lowercase} for the definition of ``lowercase letter,''
125and a convention of using two spaces after a period to separate
126sentences on the same line, it is specific to English-language texts.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000127\end{memberdesc}
128
Fred Drakeca23ee22002-07-02 20:37:12 +0000129\begin{memberdesc}{break_long_words}
Greg Ward285f4a72002-07-02 21:48:12 +0000130(default: \code{True}) If true, then words longer than
Fred Drake228f6e42002-07-03 05:08:48 +0000131\member{width} will be broken in order to ensure that no lines are
132longer than \member{width}. If it is false, long words will not be
133broken, and some lines may be longer than \member{width}. (Long words
134will be put on a line by themselves, in order to minimize the amount
135by which \member{width} is exceeded.)
Greg Wardae64f3ad2002-06-29 02:38:50 +0000136\end{memberdesc}
137
138\class{TextWrapper} also provides two public methods, analogous to the
139module-level convenience functions:
140
141\begin{methoddesc}{wrap}{text}
Fred Drake228f6e42002-07-03 05:08:48 +0000142Wraps the single paragraph in \var{text} (a string) so every line is
143at most \member{width} characters long. All wrapping options are
144taken from instance attributes of the \class{TextWrapper} instance.
145Returns a list of output lines, without final newlines.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000146\end{methoddesc}
147
148\begin{methoddesc}{fill}{text}
149Wraps the single paragraph in \var{text}, and returns a single string
150containing the wrapped paragraph.
151\end{methoddesc}