PyDocs

Nav

Text Processing Services

TextWrapper

  • TextWrapper(**kwargs)
Object for wrapping/filling text.  The public interface consists of
the wrap() and fill() methods; the other methods are just there for
subclasses to override in order to tweak the default behaviour.
If you want to completely replace the main wrapping algorithm,
you'll probably have to override _wrap_chunks().

Several instance attributes control various aspects of wrapping:
  width (default: 70)
    the maximum width of wrapped lines (unless break_long_words
    is false)
  initial_indent (default: "")
    string that will be prepended to the first line of wrapped
    output.  Counts towards the line's width.
  subsequent_indent (default: "")
    string that will be prepended to all lines save the first
    of wrapped output; also counts towards each line's width.
  expand_tabs (default: true)
    Expand tabs in input text to spaces before further processing.
    Each tab will become 0 .. 'tabsize' spaces, depending on its position
    in its line.  If false, each tab is treated as a single character.
  tabsize (default: 8)
    Expand tabs in input text to 0 .. 'tabsize' spaces, unless
    'expand_tabs' is false.
  replace_whitespace (default: true)
    Replace all whitespace characters in the input text by spaces
    after tab expansion.  Note that if expand_tabs is false and
    replace_whitespace is true, every tab will be converted to a
    single space!
  fix_sentence_endings (default: false)
    Ensure that sentence-ending punctuation is always followed
    by two spaces.  Off by default because the algorithm is
    (unavoidably) imperfect.
  break_long_words (default: true)
    Break words longer than 'width'.  If false, those words will not
    be broken, and some lines might be longer than 'width'.
  break_on_hyphens (default: true)
    Allow breaking hyphenated words. If true, wrapping will occur
    preferably on whitespaces and right after hyphens part of
    compound words.
  drop_whitespace (default: true)
    Drop leading and trailing whitespace from lines.
  max_lines (default: None)
    Truncate wrapped lines.
  placeholder (default: ' [...]')
    Append to the last line of truncated text.
Members of TextWrapper

b

break_long_words
(default: True) If true, then words longer than width will be broken in order to ensure that no lines are longer than width.
break_on_hyphens
(default: True) If true, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English.

d

drop_whitespace
(default: True) If true, whitespace at the beginning and ending of every line (after wrapping but before indenting) is dropped.

e

expand_tabs
(default: True) If true, then all tab characters in text will be expanded to spaces using the expandtabs() method of text.

f

fill
Reformat the single paragraph in 'text' to fit in lines of no more than 'self.width' columns, and return a new string containing the entire wrapped paragraph.
fix_sentence_endings
(default: False) If true, TextWrapper attempts to detect sentence endings and ensure that sentences are always separated by exactly two spaces.

i

initial_indent
(default: '') String that will be prepended to the first line of wrapped output.

m

max_lines
(default: None) If not None, then the output will contain at most max_lines lines, with placeholder appearing at the end of the output.

p

placeholder
(default: ' [...]') String that will appear at the end of the output text if it has been truncated.

r

replace_whitespace
(default: True) If true, after tab expansion but before wrapping, the wrap() method will replace each whitespace character with a single space.

s

subsequent_indent
(default: '') String that will be prepended to all lines of wrapped output except the first.

t

tabsize
(default: 8) If expand_tabs is true, then all tab characters in text will be expanded to zero or more spaces, depending on the current column and the given tab size.

w

width
(default: 70) The maximum length of wrapped lines.
wrap
Reformat the single paragraph in 'text' so it fits in lines of no more than 'self.width' columns, and return a list of wrapped lines.
0 Examples
No examples for textwrap/TextWrapper.
See Also
No see-alsos for textwrap/TextWrapper
0 Notes
No notes for TextWrapper