How do I wrap text?

Brad Howes bradh at
Sun Feb 6 08:05:43 CET 2000

Doug Sauder <dwsauder at> writes:

> It seems like there should be some very easy way to wrap long lines of
> text.

Below is something I've used to format auto-generated email messages. The Sink
class referred to is simply a class wrapper around the 'array' module.

# TextFill -- reformats a text block so that each line is no longer than the
# indicated margin value. Understands some indentation.
def TextFill( text, margin = 72 ):

	# Intialize accumulator for a paragraph of text.
	sink = Sink.New()
	count = 0
	indent = ''

	# Visit each line of text. Append as much as we can to the current
	# line until we reach a margin limit, where a new line is formed. If
	# there is an empty line, a new paragraph is started.
	for eachLine in splitfields( text, '\n' ):

		# Do we have a paragraph separator?
		if len( strip( eachLine ) ) == 0:

			# Finish off any active paragraph
			if count > 0:
				sink << '\n\n'
				count = 0
				indent = ''

		# Normal processing.

			# If the line starts with a space, assume we have indentation.
			# Don't wrap it.
			if eachLine[ 0 ] == ' ':
				tmp = 0
				while eachLine[ tmp ] == ' ':
					tmp = tmp + 1

				indent = eachLine[ : tmp ]
				if count == 0:
					sink << indent

			elif eachLine[ 0 ] == '*':
				indent = ''

			# Copy over words from the line
			for eachWord in split( eachLine ):

				# Finish line if we have reached the margin.
				wordLen = len( eachWord )
				if count > 0 and count + wordLen > margin:
					sink << '\n' << indent
					count = len( indent )

				# Add word to current paragraph
				count = count + wordLen + 1
				sink << eachWord << ' '

	# Close any remaining paragraph.
	if count > 0:
		sink << '\n'

	return sink.Value()

def Test():
	tmp = '''Four score and seven years ago our fathers brought forth on
this continent a new nation, conceived in liberty and dedicated to the  
proposition that all men are created equal. Now we are engaged in  
a great civil war, testing whether that nation or any nation so  
conceived and so dedicated can long endure. We are met on a great  
battlefield of that war. We have come to dedicate a portion of  
that field as a final resting-place for those who here gave their  
lives that that nation might live. It is altogether fitting and  
proper that we should do this. But in a larger sense, we cannot  
dedicate, we cannot consecrate, we cannot hallow this ground.  
The brave men, living and dead who struggled here have consecrated  
it far above our poor power to add or detract. The world will  
little note nor long remember what we say here, but it can never  
forget what they did here. It is for us the living rather to be  
dedicated here to the unfinished work which they who fought here  
have thus far so nobly advanced. It is rather for us to be here  
dedicated to the great task remaining before us--that from these  
honored dead we take increased devotion to that cause for which  
they gave the last full measure of devotion--that we here highly  
resolve that these dead shall not have died in vain, that this  
nation under God shall have a new birth of freedom, and that  
government of the people, by the people, for the people shall  
not perish from the earth.'''
	print Fill( tmp, 20 )
	print Fill( tmp, 40 )
	print Fill( tmp, 72 )

    "Were people this stupid before TV?" -- _White Noise_ by Don DeLillo

More information about the Python-list mailing list