How to Remove Blank Page in Word using Python

This article describes how to remove blank page in Word using Python. It has the details to set the environment, a list of steps, and a runnable sample code showing how to delete empty page in Word using Python. The sample code can be used as it is to perform the task however, you may improvise it by adding different conditions if required.

Steps to Delete the Blank Page in Word using Python

  1. Set the development environment to use Aspose.Words for Python via .NET to delete the blank pages
  2. Load the source Word file into the Document object having blank pages in it
  3. Iterate through all the pages in the document and check each section’s content
  4. Check the presence of text and images and prepare a list of empty pages
  5. Create a new empty Word document
  6. Append all the pages having some content on it from the source Word file using the blank pages list
  7. Save the resultant Word file having no blank page

These steps explain the process of how to remove blank Word page using Python. In this process, the source Word file is loaded and all the sections in each page are examined for having text or images to prepare a list of blank pages. In the end, a new document is created and all the pages other than the blank pages are added to this new empty document.

Code to Remove Blank Page in Word using Python

This sample code demonstrates how to delete empty page from Word using Python. In this sample code, the body text and node type SHAPE are tested for each section however you may use other types like TABLE, COMMENT, FORM_FIELD, SMART_TAG, etc. to identify the blank pages. The append_document() method is used for adding pages to the new Word file whereas it contains various overloaded methods allowing different arguments for the customization of the page insertion process.

This article has taught us how to remove empty page in Word using Python. If you want to put page numbers in a Word file, refer to the article on how to put page number in Word using Python.

 English