Skip to content
Related Articles

Related Articles

Working with Paragraphs in Python .docx Module
  • Last Updated : 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.

Adding a Paragraph

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-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding paragraph
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 
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-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding multilined paragraph
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 
doc.save('gfg.docx')

Output:

Document gfg.docx, you can see that enters are preserved in the paragraph

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. 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-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding Normal Texted paragraph
doc.add_heading('Normal Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Normal')
  
# Adding Body Text Styled paragraph
doc.add_heading('Body Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Body Text')
  
# Adding Caption Styled paragraph
doc.add_heading('Caption Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Caption')
  
# Adding Title Styled paragraph
doc.add_heading('Title Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Title')
  
# Adding Heading Styled paragraph
doc.add_heading('Heading 1 Style:', 3)
doc.add_paragraph('GeeksforGeeks 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('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'macro')
  
# Adding Quoted Style paragraph
doc.add_heading('Quoted Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Quote')
  
# Adding TOC Heading Styled paragraph
doc.add_heading('TOC Heading Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'TOC Heading')
  
# Adding Subtitle Styled paragraph
doc.add_heading('Subtitle Style:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.',
                  style = 'Subtitle')
  
# Adding No Spacing Styled paragraph
doc.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:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :