How to delete any object in a Word DOCX document in Python

Today’s tutorial will cover deleting specific objects from a DOCX file, such as an image, paragraph, or table. This is going to be an absolute breeze, just you wait.

Pip install our API client to begin.

pip install cloudmersive-convert-api-client

Now call edit_document_docx_remove_object, like so:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))req_config = cloudmersive_convert_api_client.DocxRemoveObjectRequest() # DocxRemoveObjectRequest | Document input requesttry:# Delete any object in a Word DOCX documentapi_response = api_instance.edit_document_docx_remove_object(req_config)pprint(api_response)except ApiException as e:print("Exception when calling EditDocumentApi->edit_document_docx_remove_object: %s\n" % e)

This takes in a request object, which will contain the file URL or byte array, as well as the path to the object that should be removed. To get the object path, you may use the functions Get Images, Get Body, etc. Once you submit your request, your file will be processed. Then you can download the finished file after calling Finish Editing. That’s it!

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store