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( 'w3wiki' , 0 ) # Adding paragraph with alignment Center doc.add_heading( 'Alignment: Center' , 3 ) para = doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' ) para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # Adding paragraph with alignment Left doc.add_heading( 'Alignment: Left' , 3 ) para = doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' ) para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT # Adding paragraph with alignment Right doc.add_heading( 'Alignment: Right' , 3 ) para = doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' ) para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT # Adding paragraph with alignment Justify doc.add_heading( 'Alignment: Justify' , 3 ) para = doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' ) para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY # Adding paragraph with alignment Distribute doc.add_heading( 'Alignment: Distribute' , 3 ) para = doc.add_paragraph( 'w3wiki 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:
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.