Working with Paragraphs in Python .docx Module - GeeksforGeeks

03 Jan, 2021
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.
To add a paragraph in a word document we make use the inbuilt method add_paragraph() to add a paragraph in the word document. By using this method we can even add paragraphs which have characters like ‘\n’, ‘\t’ and ‘\r’. Apart from that, we can also add various styles to it.
Syntax: doc.add_paragraph(String s, style=None)
Parameters:
String s: It is the string data that is to be added as a paragraph. This string can contain newline character ‘\n’, tabs ‘\t’ or a carriage return character ‘\r’.
style: It is used to set style.
Example 1: Python program to add a paragraph in a Word document.
Python3
# Import docx NOT python-docximport docx  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding paragraphdoc.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 doc.save('gfg.docx')
Output:
A multiline paragraph can be inserted by giving a multiline string input in the method, which can be done easily by using three single quotes ”’ Geeksforgeeks ”’.
Example 2: Python program to add multiline paragraphs in a word document.
Python3
# Import docx NOT python-docximport docx  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding multilined paragraphdoc.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 doc.save('gfg.docx')
Output:
Document gfg.docx, you can see that enters are preserved in the paragraph
They are used to add styles to the text of the paragraphs. You can add headings, captions, Quotes and also titles using paragraph styles. The 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-docximport docx  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding Normal Texted paragraphdoc.add_heading('Normal Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Normal')  # Adding Body Text Styled paragraphdoc.add_heading('Body Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Body Text')  # Adding Caption Styled paragraphdoc.add_heading('Caption Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Caption')  # Adding Title Styled paragraphdoc.add_heading('Title Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Title')  # Adding Heading Styled paragraphdoc.add_heading('Heading 1 Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Heading 1')  # Adding Macro Text Styled paragraphdoc.add_heading('Macro Text Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'macro')  # Adding Quoted Style paragraphdoc.add_heading('Quoted Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Quote')  # Adding TOC Heading Styled paragraphdoc.add_heading('TOC Heading Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'TOC Heading')  # Adding Subtitle Styled paragraphdoc.add_heading('Subtitle Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'Subtitle')  # Adding No Spacing Styled paragraphdoc.add_heading('No Spacing Style:', 3)doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',                  style = 'No Spacing')  # Now save the document to a location doc.save('gfg.docx')
Output:
Python Docx-module
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python