Welcome, guest | Sign In | My Account | Store | Cart

I needed to send out emails about content and when quoting the content I wanted to indent it with a few spaces. (see example output below) This recipe is clever in that it also allows for a maxwidth parameter so that the indentation is never broken.

Python, 41 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def LineIndent(text, indent, maxwidth=None):
    """ indent each new line with 'indent' """
    if maxwidth:
        parts = []
        for part in text.split('\n'):
            words = part.split(' ')
            lines = []
            tmpline = ''
            for word in words:
                if len(tmpline+' '+word) > maxwidth:
                    lines.append(tmpline.strip())
                    tmpline = word
                else:
                    tmpline += ' ' + word
                
            lines.append(tmpline.strip())
            start = "\n%s"%indent
            parts.append(indent + start.join(lines))
        return "\n".join(parts)
    else:
        text = indent+text
        text = text.replace('\n','\n%s'%indent)
    return text



def test__LineIndent():
    t='''There seems to be a problem with paragraphs that are long and on '''\
       '''multiple lines. Now there is a simple solution to this.

First you go to ASPN and look at this example, then you download it and use '''\
'''it in your own scripts. Let's hope you're lucky.'''
    
    print LineIndent(t, '*'*4, maxwidth=50)
    print "--------------------------------"
    print LineIndent(t, '> ', maxwidth=35)
    print "--------------------------------"    
    print LineIndent(t, '*'*4)
    
if __name__=='__main__':
    test__LineIndent()

Because spaces aren't shown nicely here, I've used '*' instead of ' ' for the output::

*There seems to be a problem with paragraphs that *are long and on multiple lines. Now there is a **simple solution to this.


*First you go to ASPN and look at this example, *then you download it and use it in your own

**scripts. Let's hope you're lucky.

There seems to be a problem with paragraphs that are long and on multiple lines. Now there is a simple solution to this.

First you go to ASPN and look at this example, then you download it and use it in your own scripts.

> Let's hope you're lucky.

**There seems to be a problem with paragraphs that are long and on multiple lines. Now there is a simple solution to this.


**First you go to ASPN and look at this example, then you download it and use it in your own scripts. Let's hope you're lucky.

3 comments

jemfinch 17 years ago  # | flag

Not to rain on your parade or anything, but is there a good reason to use this instead of the included textwrap module? It has several options for handling indentation.

textwrap? I don't think textwrap supports handling multiple paragraphs.

Hamish Lawson 16 years, 10 months ago  # | flag

Splitting into paragraphs. Then perhaps the problem you may want to focus on instead is that of splitting a text into a sequence of paragraphs, which you then feed in turn to 'textwrap'. That way you wouldn't be duplicating the functionality that 'textwrap' already provides.

Created by Peter Bengtsson on Wed, 10 Nov 2004 (PSF)
Python recipes (4591)
Peter Bengtsson's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks