Python – Reformatting Paragraphs

Formatting of paragraphs is needed when we deal with large amount of text and bring it to a presentable format. We may just want to print each line with specific width or try to increase the indentation for each next line when printing a poem. In this chapter we use a module named as textwrap3 to format the paragraphs as needed.

First we need to install the required package as follows

 pip install textwrap3 

Wrapping to a Fixed Width

In this example we specify a width of 30 characters in each line for a paragraph. Use the wrap function by specifying a value for the width parameter.

from textwrap3 import wrap

text = 'In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day.'

x = wrap(text, 30)
for i in range(len(x)):
    print(x[i])

When we run the above program we get the following output −

>In late summer 1945, guests
are gathered for the wedding
reception of Don Vito
Corleones daughter Connie
(Talia Shire) and Carlo Rizzi
(Gianni Russo). Vito (Marlon
Brando), the head of the
Corleone Mafia family, is
known to friends and
associates as Godfather. He
and Tom Hagen (Robert Duvall),
the Corleone family lawyer,
are hearing requests for
favors because, according to
Italian tradition, no Sicilian
can refuse a request on his
daughters wedding day.

Variable Indention

In this example we increase the indent for each line of a poem to be printed.


import textwrap3

FileName = ("path\poem.txt")

print("**Before Formatting**")
print(" ")

data=file(FileName).readlines()
for i in range(len(data)):
   print data[i]
   
print(" ")
print("**After Formatting**")
print(" ")
data=file(FileName).readlines()
for i in range(len(data)):
   dedented_text = textwrap3.dedent(data[i]).strip()
   print dedented_text

When we run the above program we get the following output −

>**Before Formatting**

 Summer is here.
  Sky is bright.
	Birds are gone.
	 Nests are empty.
	  Where is Rain?

**After Formatting**
 
Summer is here.
Sky is bright.
Birds are gone.
Nests are empty.
Where is Rain?