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)
Working with Page Orientations and Pagination Properties – 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 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.
Contact Us