Saturday, November 16, 2024
Google search engine
HomeLanguagesParagraph Formatting In Python .docx Module

Paragraph Formatting In Python .docx Module

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.

Line Spacing

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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with spacing
doc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
# Adding linspace of 0.5 inches in the paragraph
para.paragraph_format.line_spacing = Inches(0.5)
 
# Adding paragraph without spacing
doc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar. 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-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with spacing
doc.add_heading('Line Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
# Adding line space in the paragraph
para.paragraph_format.line_spacing = 1.75
 
# Adding paragraph without spacing
doc.add_heading('Line Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar. 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:

Paragraph Spacing

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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with spacing
doc.add_heading('Paragraph Spacing: Para-1 [With Spacing]', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
# Adding space before and after of the paragraph
para.paragraph_format.space_before = Inches(0.25)
para.paragraph_format.space_after = Inches(0.25)
 
# Adding paragraph without spacing
doc.add_heading('Paragraph Spacing: Para-2 [Without Spacing]', 3)
doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
 
# Now save the document to a location
doc.save('gfg.docx')


Output:

Horizontal Alignment

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, Center, right, or fully justified.

Various alignments are described below:

Sr. No.

Alignment Name

Description

1.

CENTER

It sets the alignment to Center.

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-docx
import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with alignment Center
doc.add_heading('Alignment: Center', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
 
# Adding paragraph with alignment Left
doc.add_heading('Alignment: Left', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
 
# Adding paragraph with alignment Right
doc.add_heading('Alignment: Right', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
 
# Adding paragraph with alignment Justify
doc.add_heading('Alignment: Justify', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
 
# Adding paragraph with alignment Distribute
doc.add_heading('Alignment: Distribute', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar.')
para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.DISTRIBUTE
 
# Now save the document to a location
doc.save('gfg.docx')


Output:

Indentations

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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal \
for Lazyroar. 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 Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal\
for Lazyroar. 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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with negative left Indentation
doc.add_heading('Indentation: Left', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal \
for Lazyroar. 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 Indentation
doc.add_heading('Indentation: Right', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal\
for Lazyroar. 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:

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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science\
portal for Lazyroar. It contains well written, well thought and \
well-explained computer science and programming articles, quizzes etc.')
 
# Causing First Line Indentation
para.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-docx
import docx
from docx.shared import Inches
 
# Create an instance of a word document
doc = docx.Document()
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Adding paragraph with only first line Indented
doc.add_heading('Indentation: First Line', 3)
para = doc.add_paragraph('Lazyroar is a Computer Science portal for Lazyroar. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.')
 
# Causing First Line Indentation
para.paragraph_format. first_line_indent = Inches(-0.5)
 
# Now save the document to a location
doc.save('gfg.docx')


Output:

RELATED ARTICLES

Most Popular

Recent Comments