Convert Excel to CSV using REST API in Python

In this article, we will explore various quick and efficient ways to export data from Excel to CSV on the cloud. We use Excel files to maintain invoices, ledgers, inventory, accounts, and other data in tabular form. On the other hand, a CSV (comma-separated values) file stores tabular data (numbers and text) as plain text and uses a comma to separate values. In CSV, each line in a file is a data record and each record consists of one or more fields, separated by commas. Excel to CSV conversion allows importing data to other applications. This article will be focusing on how to convert Excel files to CSV using a REST API in Python.

The following topics shall be covered in this article:

Excel to CSV Conversion REST API and Python SDK

For converting XLSX to CSV, we will be using the Python SDK of GroupDocs.Conversion Cloud API. It allows us to seamlessly convert documents and images of any supported file format to any format we require. Please install it using the following command in the console:

pip install groupdocs_converison_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Convert Excel to CSV using a REST API in Python

We can easily convert Excel files to CSV on the cloud by following the simple steps given below:

  1. Upload the XLSX file to the cloud.
  2. Convert Excel to CSV.
  3. Download the converted CSV file.

Upload the Excel File

Firstly, we will upload the XLSX file to the cloud using the following code sample:

As a result, the uploaded XLSX file will be available in the files section of the dashboard on the cloud.

Convert Excel to CSV in Python

Now, we will convert the uploaded XLSX to CSV programmatically by following the steps given below:

  • Create an instance of the ConvertApi.
  • Initialize the ConvertSettings object.
  • Set the XLSX file path.
  • Assign “csv” to format.
  • Provide the output file path.
  • Create ConvertDocumentRequest with ConvertSettings.
  • Convert by calling the convert_document() method.

The following code sample shows how to convert an Excel file to a CSV using a REST API in Python.

Convert Excel to CSV using a REST API in Python.

Convert Excel to CSV using a REST API in Python.

Download the Converted File

The above code sample will save the converted CSV file on the cloud. It can be downloaded using the following code sample:

Convert Excel to CSV and Download File Directly

We can convert XLSX to CSV and download the converted CSV file directly by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, define ConvertSettings and set the uploaded XLSX file path.
  • Then, assign “csv” to format.
  • Set the output file path as None.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the convert_document_download() method to save the converted file on local disk.

The following code sample shows how to convert an Excel file to a CSV and download it directly using a REST API in Python. The API shall return the converted CSV file in response. Please follow the steps mentioned earlier to upload a file.

Excel to CSV Conversion without using Cloud Storage

We can also convert an Excel file to CSV without using cloud storage by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create ConvertDocumentDirectRequest with target format and input XLSX file path as arguments.
  • Then, call the convert_document_direct() method with ConvertDocumentDirectRequest as an argument.
  • Finally, save the converted output CSV file to the local path using FileStream.writeFile() method.

The following code sample shows how to convert XLSX to CSV without using cloud storage in Python. It means we will pass the input file in the request body and receive the output file in the API response.

Convert CSV to Excel using a REST API in Python

We can also export comma-separated data from a CSV into a well-formatted Excel file on the cloud. For converting CSV to Excel, please follows the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create ConvertDocumentDirectRequest with target format and input CSV file path as arguments.
  • Then, call the convert_document_direct() method with ConvertDocumentDirectRequest as an argument.
  • Finally, save the converted output XLSX file to the local path using FileStream.writeFile() method.

The following code sample shows how to convert a CSV to an Excel file using a REST API in Python.

Try Online

Please try the following free online XLSX to CSV and CSV to XLSX conversion tools, which are developed using the above API.

Conclusion

In this article, we have learned how to:

  • convert Excel to CSV and CSV to Excel in Python;
  • convert XLSX to CSV and download the converted file directly;
  • XLSX to CSV conversion without using cloud storage;
  • programmatically upload XLSX file to the cloud;
  • download CSV file from the cloud.

Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also