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.
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.
section.orientation = WD_ORIENT.[Orientation Type]
There are two types of orientations possible.
It is used to set the orientation to portrait.
It is used to set the orientation to landscape.
- 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.
Default Orientation: PORTRAIT (0)
Example 2: Changing the orientation to the landscape from default.
Default Orientation: PORTRAIT (0) New Orientation: LANDSCAPE (1)
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.
doc.add_paragraph(String s, style=None)
- 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:
Keeps the content of the paragraph on one page.
Keeps the content of a paragraph with another subsequent paragraph.
Move a paragraph to a new page because of a page break.
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.
Example 4: Using keep_with_next on a paragraph in a word document.
Example 5: Using page_break_before on a paragraph in a word document.
Example 6: Using widow_control on a paragraph in a Word document.