How to Convert Multiple CSV Files to Excel in Python
When you’re looking for basic data storage formats, the two most popular options are CSV and Excel. While they are both excellent choices for data storage, their benefits vary depending on the type of project you’re working on; CSV is best when you need to parse the data, but Excel is better for adding advanced features such as macros and graphs. If you created several files with CSV and encounter a situation that requires the data be organized into a graph, you will need to convert the files to the Excel. In this tutorial, we will share an API that can be used in Python to convert multiple CSV files to a single XLSX spreadsheet, allowing you to simplify the process.
Let’s kick things off by running this command to install the SDK:
pip install cloudmersive-convert-api-client
After the installation, we are ready to call the function with the following code:
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_file1 = '/path/to/inputfile' # file | First input file to perform the operation on.
input_file2 = '/path/to/inputfile' # file | Second input file to perform the operation on.
input_file3 = '/path/to/inputfile' # file | Third input file to perform the operation on. (optional)
input_file4 = '/path/to/inputfile' # file | Fourth input file to perform the operation on. (optional)
input_file5 = '/path/to/inputfile' # file | Fifth input file to perform the operation on. (optional)
input_file6 = '/path/to/inputfile' # file | Sixth input file to perform the operation on. (optional)
input_file7 = '/path/to/inputfile' # file | Seventh input file to perform the operation on. (optional)
input_file8 = '/path/to/inputfile' # file | Eighth input file to perform the operation on. (optional)
input_file9 = '/path/to/inputfile' # file | Ninth input file to perform the operation on. (optional)
input_file10 = '/path/to/inputfile' # file | Tenth input file to perform the operation on. (optional)
worksheet_names = 'worksheet_names_example' # str | Optional; Specify the name of each CSV's worksheet in order, separated with commas (e.g. \"worksheet1,worksheet2,worksheet3\"). Defaults to the names of the input CSV files. Recommended when inputting the files directly, without file names. (optional)try:
# Convert Multiple CSV Files into a Single XLSX Spreadsheet
api_response = api_instance.convert_document_csv_multi_to_xlsx(input_file1, input_file2, input_file3=input_file3, input_file4=input_file4, input_file5=input_file5, input_file6=input_file6, input_file7=input_file7, input_file8=input_file8, input_file9=input_file9, input_file10=input_file10, worksheet_names=worksheet_names)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_csv_multi_to_xlsx: %s\n" % e)
At the end of this quick operation, your new Excel file will be delivered! Each CSV file will be represented by a worksheet within the document, ensuring easy identification of the original data.