How to merge two Excel XLSX files together in Python
Excel spreadsheet merging. It’s not flashy or exciting, but it can be very important for productivity. So, Python users, we have two options, the easy way and the hard way. Obviously we are going to go the easy route, so it’s time to bust out an API. We’ll all be home in time for supper.
First off, we shall start the install for our client:
pip install cloudmersive-convert-api-client
When that’s finished, we write out the code that will create our API instance, then call the merge_document_xlsx_multi function.
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.MergeDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file1 = '/path/to/file' # file | First input file to perform the operation on.input_file2 = '/path/to/file' # file | Second input file to perform the operation on.input_file3 = '/path/to/file' # file | Third input file to perform the operation on. (optional)input_file4 = '/path/to/file' # file | Fourth input file to perform the operation on. (optional)input_file5 = '/path/to/file' # file | Fifth input file to perform the operation on. (optional)input_file6 = '/path/to/file # file | Sixth input file to perform the operation on. (optional)input_file7 = '/path/to/file' # file | Seventh input file to perform the operation on. (optional)input_file8 = '/path/to/file' # file | Eighth input file to perform the operation on. (optional)input_file9 = '/path/to/file' # file | Ninth input file to perform the operation on. (optional)input_file10 = '/path/to/file' # file | Tenth input file to perform the operation on. (optional)try:# Merge Multple Excel XLSX Togetherapi_response = api_instance.merge_document_xlsx_multi(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)pprint(api_response)except ApiException as e:print("Exception when calling MergeDocumentApi->merge_document_xlsx_multi: %s\n" % e)
As you can see, we may specify up to 10 individual XLSX files to be used for our merge. If you require more files than that, simply continue calling the function using the previous response as your input_file1 to preserve the order.