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( 'w3wiki' , 0 ) # Adding paragraph with left Indentation doc.add_heading( 'Indentation: Left' , 3 ) para = doc.add_paragraph('w3wiki 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 Indentation doc.add_heading( 'Indentation: Right' , 3 ) para = doc.add_paragraph('w3wiki 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-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( 'w3wiki' , 0 ) # Adding paragraph with negative left Indentation doc.add_heading( 'Indentation: Left' , 3 ) para = doc.add_paragraph('w3wiki 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 Indentation doc.add_heading( 'Indentation: Right' , 3 ) para = doc.add_paragraph('w3wiki 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:
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( 'w3wiki' , 0 ) # Adding paragraph with only first line Indented doc.add_heading( 'Indentation: First Line' , 3 ) para = doc.add_paragraph('w3wiki 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 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( 'w3wiki' , 0 ) # Adding paragraph with only first line Indented doc.add_heading( 'Indentation: First Line' , 3 ) para = doc.add_paragraph( 'w3wiki 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 Indentation para.paragraph_format. first_line_indent = Inches( - 0.5 ) # 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.
Contact Us