Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active July 28, 2022 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/2da251611767895223a0eb5eb1f469e3 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/2da251611767895223a0eb5eb1f469e3 to your computer and use it in GitHub Desktop.
Convert CSV to Excel XLS/XLSX Spreadsheet in Python

Learn how to convert CSV to Excel document in Python using REST API:

The following topics shall be covered in this article:

  1. CSV to Excel XLS/XLSX Rest API and Python SDK
  2. How to Convert CSV File to Excel XLSX in Python
  3. How to Convert CSV to XLS Spreadsheet in Python
  4. CSV File Converter Online Free
# Load Python library for CSV to Excel conversion using python
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get different configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "groupdocs-cloud_storage"
# Upload sample .csv file on the cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\Sample-Spreadsheet-500000-rows.csv", "H:\\groupdocs-cloud-data\\Sample-Spreadsheet-500000-rows.csv", storage_name)
# Upload sample CSV file
response = file_api.upload_file(request)
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\Sample-Spreadsheet-500000-rows.xlsx", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# How to Convert CSV to XLS Spreadsheet in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Set and define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/Sample-Spreadsheet-500000-rows.csv"
settings.format = "xls"
loadOptions = groupdocs_conversion_cloud.CsvLoadOptions()
loadOptions.separator = ","
convertOptions = groupdocs_conversion_cloud.XlsConvertOptions()
settings.load_options = loadOptions
settings.convert_options = convertOptions
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert csv to excel xls spreadsheet
response = convert_api.convert_document(request)
print("Successfully converted Word DOCX to JPEG image format: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
# How to Convert CSV File to Excel XLSX in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/Sample-Spreadsheet-500000-rows.csv"
settings.format = "xlsx"
loadOptions = groupdocs_conversion_cloud.CsvLoadOptions()
loadOptions.separator = ","
convertOptions = groupdocs_conversion_cloud.XlsxConvertOptions()
settings.load_options = loadOptions
settings.convert_options = convertOptions
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert csv to excel xlsx file spreadsheet
response = convert_api.convert_document(request)
print("Successfully converted Word DOCX to JPEG image format: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment