Working With Text In Python .docx Module
Last Updated :
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 user 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. You can also manipulate the font size, colour and its style using this module.
Font Size
To increase/decrease the font size of the text you have to first create a paragraph object then you have to use add_run() method to add content. You can directly use add_paragraph() method to add paragraph but if you want to increase/decrease the font size of the text you have to use add_run() as all the block-level formatting is done by using add_paragraph() method while all the character-level formatting is done by using add_run().
Now to set a new font size we will use .font.size method. This is the method of the font object and is used to set the new font size of the text.
Syntax: para.font.size = Length
Parameter:
Length: It defines the size of the font. It can be in inches, pt or cm.
Example 1: Setting the font size of the text in a paragraph.
Python3
import docx
from docx.shared import Pt
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
doc.add_heading('Increased Font Size Paragraph:', 3)
para = doc.add_paragraph().add_run(
'GeeksforGeeks is a Computer Science portal for geeks.')
para.font.size = Pt(12)
doc.add_heading('Normal Font Size Paragraph:', 3)
doc.add_paragraph(
'GeeksforGeeks is a Computer Science portal for geeks.')
doc.save('gfg.docx')
|
Output:

Font Colour
To apply a font colour to the text you have to first create a paragraph object then you have to use add_run() method to add content. You can directly use add_paragraph() method to add paragraph but if you want to apply a font colour to a text you have to use add_run() as all the block-level formatting is done by using add_paragraph() method while all the character-level formatting is done by using add_run().
To set the colour to the font we will make us of the RGBColor() object which takes hexadecimal input of the colour and sets the same colour to the text.
Syntax: para.font.color.rgb = RGBColor([RGB Colour Value in Hexadecimal])
Parameter:
RGB Colour Value: It is the hexadecimal value of the colour you want to set. It is given in the form of R, G, B as input.
Note: You have to add ‘from docx.shared import RGBColor‘ import statement before calling RGBColor() function in your code.
Example 2: Adding colour to the text in the paragraph.
Python3
import docx
from docx.shared import RGBColor
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
doc.add_heading('Font Colour:', 3)
para = doc.add_paragraph().add_run(
'GeeksforGeeks is a Computer Science portal for geeks.')
para.font.color.rgb = RGBColor(0x22, 0x8b, 0x22)
doc.save('gfg.docx')
|
Output:

Font Style
To set a new font style for the text you have to first create a paragraph object then you have to use add_run() method to add content. You can directly use add_paragraph() method to add paragraph but if you want to set a new font style of the text you have to use add_run() as all the block-level formatting is done by using add_paragraph() method while all the character-level formatting is done by using add_run().
Now to set a new font name we will use .font.name method. This is the method of the font object and is used to set the new font name for the text.
Syntax: para.font.name = String s
Parameter:
String s: It is the name of the new font style. If you give any random name as input string then default style is adopted for the text.
Example 3: Setting a new font name for a paragraph.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
doc.add_heading('Font Style: Roboto', 3)
para = doc.add_paragraph().add_run(
'GeeksforGeeks is a Computer Science portal for geeks.')
para.font.name = 'Roboto'
doc.add_heading('Font Style: Default [Cambria]', 3)
doc.add_paragraph(
'GeeksforGeeks is a Computer Science portal for geeks.')
doc.save('gfg.docx')
|
Output:

Bold Text
To set the text to bold you have to set it true.
doc.bold = True
To highlight a specific word the bold needs to be set True along with its add_run() statement.
add_run(" text ").bold=True
Example 1: Applying bold to a complete paragraph.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph()
bold_para = para.add_run(
)
bold_para.bold = True
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Example 2: Applying bold to a specific word or phrase.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph(
)
para.add_run(
).bold = True
para.add_run()
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Italics Text
To set the text to italics you have to set it true.
doc.italic = True
To make some specific word(s) italics, it needs to be set True along with its add_run() statement.
add_run(" text ").italic=True
Example 3: Applying italics to a complete paragraph.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph()
italic_para = para.add_run(
)
italic_para.italic = True
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Example 4: Applying italics to a specific word or phrase.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph(
)
para.add_run(
).italic = True
para.add_run()
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Underlined Text
To apply to underline to a text you have to set it true.
doc.underline = True
To underline a specific part, underline needs to set a True along with its add_run() function
add_run("text").underline=True
Example 5: Applying underline to a complete paragraph.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph()
underline_para = para.add_run(
)
underline_para.underline = True
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Example 6: Applying underline to a specific word or phrase.
Python3
import docx
doc = docx.Document()
doc.add_heading('GeeksForGeeks', 0)
para = doc.add_paragraph(
)
para.add_run(
).underline = True
para.add_run()
doc.save('gfg.docx')
|
Output:

Document gfg.docx
Please Login to comment...