How to remove a worksheet from an Excel XLSX spreadsheet in Python

While worksheet removal from an XLSX file may at first seem rather simple and straightforward, you may quickly find that the process has its share of pitfalls and time sinks. I am going to rescue from this minefield by showing you the easy way out.

We start with pip installation of the Cloudmersive API client for working with documents and file conversions.

pip install cloudmersive-convert-api-client

Let’s now move on to writing our function call for edit_document_xlsx_delete_worksheet. You can reference the following example code to see how to instance the API using a key, which is required for said function call.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest 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.RemoveXlsxWorksheetRequest() # RemoveXlsxWorksheetRequest | Spreadsheet input requesttry:# Delete, remove worksheet from an Excel XLSX spreadsheet documentapi_response = api_instance.edit_document_xlsx_delete_worksheet(req_config)pprint(api_response)except ApiException as e:print("Exception when calling EditDocumentApi->edit_document_xlsx_delete_worksheet: %s\n" % e)

Done!

Image for post
Image for post

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