Paragraph Styles
They are used to add styles to the text of the paragraphs. You can add headings, captions, Quotes and also titles using paragraph styles. There are many styles in the paragraph method, some of them are:
Sr. No |
Style Name |
Description |
---|---|---|
1. |
Normal |
It is used to add normal text. |
2. |
Body Text |
It is used to add Body Text styled text. |
3. |
Caption |
It is used to add caption styled text. |
4. |
Title |
It is used to add Title styled text. |
5. |
Heading n |
It is used to add Heading styled text. n can be any integer from the range 1-9. |
6. |
macro |
It is used to add macro styled text. |
7. |
Quote |
It is used to add quote styled text. |
8. |
TOC Heading |
It is used to add TOC Heading styled text. |
9. |
Subtitle |
It is used to add subtitle styled text. |
10. |
No Spacing |
It is used to add text with no spacing. |
Example 3: Python program to add paragraphs with different styles in a word document.
Python3
# Import docx NOT python-docx import docx # Create an instance of a word document doc = docx.Document() # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Adding Normal Texted paragraph doc.add_heading( 'Normal Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Normal' ) # Adding Body Text Styled paragraph doc.add_heading( 'Body Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Body Text' ) # Adding Caption Styled paragraph doc.add_heading( 'Caption Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Caption' ) # Adding Title Styled paragraph doc.add_heading( 'Title Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Title' ) # Adding Heading Styled paragraph doc.add_heading( 'Heading 1 Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Heading 1' ) # Adding Macro Text Styled paragraph doc.add_heading( 'Macro Text Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'macro' ) # Adding Quoted Style paragraph doc.add_heading( 'Quoted Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Quote' ) # Adding TOC Heading Styled paragraph doc.add_heading( 'TOC Heading Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'TOC Heading' ) # Adding Subtitle Styled paragraph doc.add_heading( 'Subtitle Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'Subtitle' ) # Adding No Spacing Styled paragraph doc.add_heading( 'No Spacing Style:' , 3 ) doc.add_paragraph( 'w3wiki is a Computer Science portal for geeks.' , style = 'No Spacing' ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Working with Paragraphs in Python .docx Module
Prerequisites: docx
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.
Contact Us