Paragraph Formatting In Python .docx Module - GeeksforGeeks

03 Jan, 2021
Prerequisite: Working with .docx module
Word documents contain formatted text wrapped within three object levels. The Lowest level- run objects, middle level- paragraph objects, and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Pip command to install this module is:
pip install python-docx
Python docx module allows users to manipulate docs by either manipulating the existing one or creating a new empty document and manipulating it. It is a powerful tool as it helps you to manipulate the document to a very large extend.
To set the line spacing between the text in the paragraph we make use of the paragraph_format along with line_spacing. It is used to set the space between each line in the paragraph.
Syntax: para.paragraph_format.line_spacing = Length
Parameter: Length: It is the length of the space to be left between the lines. It takes length as an input. It can be defined either with an absolute distance value or with a relative distance value of the line-height. If the input is in pt, inches or cm then it considered them as an absolute value and if the input is in float then it is considered as a relative value.
Example 1: Setting the line spacing with absolute distance value.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with spacingdoc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')# Adding line space of 0.5 inches in the paragraphpara.paragraph_format.line_spacing = Inches(0.5)  # Adding paragraph without spacingdoc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')  # Now save the document to a location doc.save('gfg.docx')
Output:
Example 2: Setting the line spacing with relative value.
Python3
# Import docx NOT python-docximport docx  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with spacingdoc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')# Adding line space in the paragraphpara.paragraph_format.line_spacing = 1.75  # Adding paragraph without spacingdoc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')  # Now save the document to a location doc.save('gfg.docx')
Output:
To apply paragraph spacing to the paragraphs in the Word document we make use of .paragraph_format along with .space_before and .space_after. It specifies the space to be left before and after the paragraph respectively. It can only take the positive value as input, if we give any negative value it will give range error.
Sr. No.
Spacing
Description
1.
.space_before
It adds space before the paragraph in the word document.
2.
.space_after
It adds space after the paragraph in the word document.
Example 3: Adding paragraph with and without spacing in a Word document.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with spacingdoc.add_heading('Paragraph Spacing: Para-1 [With Spacing]', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')# Adding space before and after of the paragraphpara.paragraph_format.space_before = Inches(0.25)para.paragraph_format.space_after = Inches(0.25)  # Adding paragraph without spacingdoc.add_heading('Paragraph Spacing: Para-2 [Without Spacing]', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')  # Now save the document to a location doc.save('gfg.docx')
Output:
To set the horizontal alignment in the text we will use the .paragraph_format.alignment method. It is used along with WD_PARAGRAPH_ALIGNMENT to set the alignment of the paragraph. You have to import WD_PARAGRAPH_ALIGNMENT from the docx.enum.text before using it:
from docx.enum.text import WD_ALIGN_PARAGRAPH
Syntax: para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.[Alignment]
Parameter: 
Alignment: It is used to set the alignment. You can set the alignment to any of the left, Centre, right, or fully justified.
Various alignments are described below:
Sr. No.
Alignment Name
Description
1.
CENTER
It sets the alignment to Centre.
2.
LEFT
It sets the alignment to left.
3.
RIGHT
It sets the alignment to right.
4.
JUSTIFY
It sets the alignment to justify.
5.
DISTRIBUTE
It sets the characters in such a way that they fill the entire width of the paragraph.
Example 1: Adding paragraphs with different Horizontal Alignments.
Python3
# Import docx NOT python-docximport docxfrom docx.enum.text import WD_ALIGN_PARAGRAPH  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with alignment Centerdoc.add_heading('Alignment: Center', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER  # Adding paragraph with alignment Leftdoc.add_heading('Alignment: Left', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT  # Adding paragraph with alignment Rightdoc.add_heading('Alignment: Right', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT  # Adding paragraph with alignment Justifydoc.add_heading('Alignment: Justify', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY  # Adding paragraph with alignment Distributedoc.add_heading('Alignment: Distribute', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.DISTRIBUTE  # Now save the document to a location doc.save('gfg.docx')
Output:
To set the indentation in the text we will use the .paragraph_format method. To apply indentation we use left_indent and right_indent with the .paragraph_format and set the value of the indentation. You have to specify indentation with a length value i.e inches, pt or cm. You can also give a negative value as indentation which will cause the paragraph to overlap with the margin by the value specified.
Sr. No.
Indentation
Description
1.
left_indent
It sets the left indentation of the paragraph in the word file.
2.
right_indent
It sets the right indentation of the paragraph in the word file.
Syntax:
For the left indentation: para.paragraph_format.left_indent = size
For the right indentation: para.paragraph_format.right_indent = size
Parameter: 
size: It is the value by which we want indentation on our paragraph. It can be in inches, pt or cm... etc.
Example 2: Setting left and right indentation of the paragraph.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with left Indentationdoc.add_heading('Indentation: Left', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \for geeks. It contains well written, well thought and well-explained \computer science and programming articles, quizzes etc.')para.paragraph_format.left_indent = Inches(0.5)  # Adding paragraph with right Indentationdoc.add_heading('Indentation: Right', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\for geeks. It contains well written, well thought and well-explained\computer science and programming articles, quizzes etc.')para.paragraph_format.right_indent = Inches(0.5)  # Now save the document to a location doc.save('gfg.docx')
Output:
Example 3: Setting a negative value for the left and right indentation of the paragraph.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with negative left Indentationdoc.add_heading('Indentation: Left', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal \for geeks. It contains well written, well thought and well-explained\computer science and programming articles, quizzes etc.')para.paragraph_format.left_indent = Inches(-0.5)  # Adding paragraph with negative right Indentationdoc.add_heading('Indentation: Right', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal\for geeks. It contains well written, well thought and well-explained\computer science and programming articles, quizzes etc.')para.paragraph_format.right_indent = Inches(-0.5)  # Now save the document to a locationdoc.save('gfg.docx')
Output:
You can also set indentation only for the first line of the paragraph by using .paragraph_format along with  .first_line_indent property. It specifies the indentation length between the first line and the other lines.
Syntax: para.paragraph_format. first_line_indent = Length
Parameters: 
Length: It is the length to be left as indentation at the first line. A positive value will cause the line to indent while the negative value will cause hanging indentation.
Example 4: Giving positive indentation value as input for the first-line indentation.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with only first line Indenteddoc.add_heading('Indentation: First Line', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science\portal for geeks. It contains well written, well thought and \well-explained computer science and programming articles, quizzes etc.')  # Causing First Line Indentationpara.paragraph_format. first_line_indent = Inches(0.5)  # Now save the document to a location doc.save('gfg.docx')
Output:
Example 5: Giving negative indentation value as input for the first-line indentation.
Python3
# Import docx NOT python-docximport docxfrom docx.shared import Inches  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraph with only first line Indenteddoc.add_heading('Indentation: First Line', 3)para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')  # Causing First Line Indentationpara.paragraph_format. first_line_indent = Inches(-0.5)  # Now save the document to a location doc.save('gfg.docx')
Output:
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe