How to Change Page Orientation in Word Documents using Python

Change Page Orientation in Word Documents using Python

The term orientation refers to the direction in which a document is displayed for printing and reading. Word supports two types of orientation: portrait (vertical) and landscape (horizontal). Normally, the default page size of a Word document is “Letter” (8.5 x 11 inches), and the default page orientation is “Portrait”. You can change the default orientation to create a different document, such as an application form, or brochure. In certain cases, you need to change the orientation of pages in a word file, depending on the content in the Word document. In this tutorial, I’ll show you how to change page orientation in Word documents using Python.

To learn how to change the page orientation for one page or multiple pages in word to landscape or portrait, select the appropriate links given below:

API to Change Page Orientation to Landscape or Portrait - Installation

In a Word document, you can set the page orientation to portrait or landscape for the whole document, a single page, or multiple pages depending on your project content. To change the page orientation in a word file for a single page or multiple pages programmatically, I’ll be using the Python SDK of GroupDocs.Merger Cloud API. Besides changing the document orientation, this API also supports moving, swapping, removing, splitting, and extracting pages from supported document format.

You can install GroupDocs.Merger Cloud API to your Python project using the following command in the console:

pip install groupdocs_merger_cloud

Now collect Client ID and Client Secret from the dashboard to follow the below steps and available code examples. Once you have your application credentials, copy and paste below code snippet in your Python application as shown below:

Next, add the file code snippet to your project to upload the Word file to the cloud. The uploaded Word file will be available in the files section of the dashboard on the cloud. So far, you have installed the Python library and added configurations to your application. Now, you are ready to change the Word page orientation programmatically.

How to Change Page Orientation to Landscape in Word Document using Python

Please follow the steps and the code snippet mentioned below to change word page orientation to landscape programmatically:

  • Firstly, create an instance of PagesApi class
  • Next, create an instance of OrientationOptions class
  • Now, create an instance of FileInfo with the input file as a parameter
  • Then, set the output file path on the cloud
  • Provide comma separated page numbers to change the orientation
  • Set orientation mode to Landscape
  • Next, create an instance of OrientationRequest
  • Finally, change page orientation by calling the PagesApi.orientation() method with OrientationRequest options as parameter.

The following Python code snippet is for how to change the portrait orientation to landscape in Word document:

How to Change Page Orientation to Landscape in Word using Python

How to Change Page Orientation to Landscape in Word using Python

You can also use the above code example to see how to make one page landscape in word. Landscape orientation produces a page that stretches the left-to-right margins. Additionally, you can use download file code snippet to download the file on your local system.

How to Change Page Orientation from Landscape to Portrait in word in Python

Similarly, you can convert pages of a word document to portrait orientation. Please follow the steps and the code snippets mentioned below:

  • Firstly, create an instance PagesApi
  • Secondly, create OrientationOptions instance
  • Next, create an instance of FileInfo with an input file as a parameter
  • Set the output file path on the cloud
  • Provide comma-separated page numbers to change the orientation
  • Set orientation mode to Portrait
  • Next, create an instance of OrientationRequest
  • Finally, change page orientation by calling the PagesApi.orientation() method with OrientationRequest options as parameters.

The following Python code example shows how to change the portrait orientation to portrait in Word document:

How to Change Orientation of One Page in Word to Portrait using Python

How to Change Orientation of One Page in Word to Portrait using Python

The above code sample will change the orientation of word pages from landscape to portrait. This code example can also be used to change orientation of one page in word document.

Summing up

This is the end of this blog post. You can see how orientation affects the appearance and spacing of text and images. In this article, you have learned:

  • how to change the page orientation to landscape programmatically in Python;
  • how to change selected pages orientation to portrait in word using Python;

Moreover, you can try building your own application for how to make a single-page landscape that can toggle the orientation word pages online. For the details and other features of the API, you can visit the documentation guidelines.

We suggest you follow our Getting Started guide.

Finally, groupdocs.cloud is currently writing new blog articles on different file format solutions using REST API. Stay tuned for the newest updates.

Ask a question

You can let us know about your questions or queries about how to change page orientation in word on this Forum.

FAQs

How to change orientation of one page in Word programmatically?

Please follow this link to learn the Python code snippet about how to change the orientation of only one page or multiple pages in a word document.

How do I change the orientation of an entire document in word in Python?

Create an instance of PagesApi, OrientationOptions, FileInfo, set the values of the FileInfo, and invoke the PagesApi.orientation() method with OrientationRequest to change the orientation of an entire document.

How to install page orientation free download library?

You can the install Word page orientation Python library to change landscape to portrait orientation or portrait to landscape orientation programmatically using the steps mentioned here.

How do I change page orientation in windows?

Please visit this link to download the page orientation tool for free. This offline software can be used to change the orientation of documents with a single click.

See Also