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 users 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.
Page Orientations
To change the orientation of the word document we make use of the WD_ORIENT of the docx.enum.section module. And to call or set the orientation of the section we will use the orientation method of the section class.
Syntax:
section.orientation = WD_ORIENT.[Orientation Type]
There are two types of orientations possible.
Sr. No. |
Orientation Type |
Description |
---|---|---|
1. |
Portrait |
It is used to set the orientation to portrait. |
2. |
Landscape |
It is used to set the orientation to landscape. |
Note:
- The portrait is the default orientation.
- Orientation methods can only be used upon sections so to use one you have to first select a section of the Word document.
Example 1: Printing the default orientation of the Word document.
Python3
# Import docx NOT python-docx import docx # Create an instance of a word document doc = docx.Document() # Selecting a section of the document section = doc.sections[ 0 ] # Printing the default orientation. print ( "Default Orientation:" , section.orientation) |
Output:
Default Orientation: PORTRAIT (0)
Example 2: Changing the orientation to the landscape from default.
Python3
# Import docx NOT python-docx import docx from docx.enum.section import WD_ORIENT # Create an instance of a word document doc = docx.Document() # Selecting a section of the document section = doc.sections[ 0 ] # Printing the default orientation. print ( "Default Orientation:" , section.orientation) # Changing the orientation to landscape section.orientation = WD_ORIENT.LANDSCAPE # Printing the new orientation. print ( "New Orientation:" , section.orientation) |
Output:
Default Orientation: PORTRAIT (0) New Orientation: LANDSCAPE (1)
Pagination Properties
Pagination properties are the properties or styles which control the behavior of the paragraphs near the page boundaries. There are four styles of add_paragraph() function in .docx module which comes under this category.
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.
Styles that come under pagination properties are:
Sr. NO. |
Style Name |
Description |
---|---|---|
1. |
keep_together |
Keeps the content of the paragraph on one page. |
2. |
keep_with_next |
Keeps the content of a paragraph with another subsequent paragraph. |
3. |
page_break_before |
Move a paragraph to a new page because of a page break. |
4. |
widow_control |
Keeps the first and last line of the paragraph together with the rest of the paragraph. |
Note: All the four styles can either be set to true, false, or none. True means “on”, False means “off” and None means the property is inherited from the style hierarchy.
Example 3: Using keep_together on 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 para = doc.add_paragraph( 'Lazyroar is a Computer Science portal for Lazyroar.' ) # Setting keep_together as True para.keep_together = True # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 4: Using keep_with_next on 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 para = doc.add_paragraph( 'Lazyroar is a Computer Science portal for Lazyroar.' ) # Setting keep_with_next as True para.keep_with_next = True # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 5: Using page_break_before on 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 para = doc.add_paragraph( 'Lazyroar is a Computer Science portal for Lazyroar.' ) # Setting page_break_before as True para.page_break_before = True # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 6: Using widow_control on 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 para = doc.add_paragraph( 'Lazyroar is a Computer Science portal for Lazyroar.' ) # Setting widow_control as True para.widow_control = True # Now save the document to a location doc.save( 'gfg.docx' ) |
Output: