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.
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.
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
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.
It adds space before the paragraph in the word document.
It adds space after the paragraph in the word document.
Example 3: Adding paragraph with and without spacing in a Word document.
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Now save the document to a location
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:
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.
It sets the left indentation of the paragraph in the word file.
It sets the right indentation of the paragraph in the word file.
For the left indentation: para.paragraph_format.left_indent = size
For the right indentation: para.paragraph_format.right_indent = size
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.
# Import docx NOT python-docx
# Create an instance of a word document
# Add a Title to the document
# Adding paragraph with left Indentation
doc.add_heading('Indentation: Left', 3)
para =doc.add_paragraph('GeeksforGeeks isa Computer Science portal \
forgeeks. It contains well written, well thought andwell-explained \
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.
Please Login to comment...