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 NOT python-docx import docx from docx.shared import RGBColor # Create an instance of a word document doc = docx.Document() # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Adding paragraph doc.add_heading( 'Font Colour:' , 3 ) para = doc.add_paragraph().add_run( 'w3wiki is a Computer Science portal for geeks.' ) # Adding forest green colour to the text # RGBColor(R, G, B) para.font.color.rgb = RGBColor( 0x22 , 0x8b , 0x22 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Working With Text 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 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 NOT python-docx import docx from docx.shared import Pt # Create an instance of a word document doc = docx.Document() # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Adding paragraph with Increased font size doc.add_heading( 'Increased Font Size Paragraph:' , 3 ) para = doc.add_paragraph().add_run( 'w3wiki is a Computer Science portal for geeks.' ) # Increasing size of the font para.font.size = Pt( 12 ) # Adding paragraph with normal font size doc.add_heading( 'Normal Font Size Paragraph:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Contact Us