How to convert CSV to Excel XLSX Spreadsheet in Python

Converting between CSV format and XLSX can be a bit of a tricky thing to set up if you go about it in the normal manner. That’s why I suggest following this tutorial to get this accomplished in a fraction of the usual time. Let’s begin.

Our first order of business is to set up our API client via this command:

pip install cloudmersive-convert-api-client

After which we shall call our function for CSV to XLSX conversion, like so.

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 CSV to Excel XLSX Spreadsheetapi_response = api_instance.convert_document_csv_to_xlsx(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_csv_to_xlsx: %s\n" % e)

Done! This is just a taste of what this library can do, so be sure to look at the documentation for a full breakdown of all the different types of conversion and file editing that can be achieved with simple API calls like this one.

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