How to Convert Excel to CSV in Python

Cloudmersive
2 min readMar 12, 2024

--

Let’s walk through a quick Excel to CSV conversion process. We’ll be calling a free API using complementary, ready-to-run Python code examples.

To start, let’s install the client SDK using pip install:

pip install cloudmersive-convert-api-client

After that, let’s quickly turn our attention to authorization. We need a free Cloudmersive API key to authorize our API calls — this will allow a limit of 800 API calls per month.

Lastly, let’s add the imports and call the function. Let’s provide our Excel file path and (optionally) specify the output text encoding of our CSV result:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
output_encoding = 'output_encoding_example' # str | Optional, set the output text encoding for the result; possible values are UTF-8, ASCII and UTF-32. Default is UTF-8. (optional)

try:
# Convert Excel XLSX Spreadsheet to CSV, Multiple Worksheets
api_response = api_instance.convert_document_xlsx_to_csv_multi(input_file, output_encoding=output_encoding)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_xlsx_to_csv_multi: %s\n" % e)

If our Excel document contains multiple worksheets, we’ll get one CSV file per worksheet.

That’s all there is to it — no more code required.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet