How to convert Legacy Excel XLS to CSV in Python

Are you stuck dealing with old XLS spreadsheets from 2003? This can cause all sorts of annoying problems, so it is generally better to just update these files to something better like CSV format. The library we will be using today also supports directly converting between XLS and XLSX, but that’s a different article.

Installation of the client goes like this:

pip install cloudmersive-convert-api-client

From this point, we will be creating our API instance and then calling convert_document_xls_to_csv using that instance. Here’s the code for that.

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.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Convert Excel XLS (97-03) Spreadsheet to CSVapi_response = api_instance.convert_document_xls_to_csv(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_xls_to_csv: %s\n" % e)

And really, that’s it. Like shooting fish in a barrel.

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