Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge Multiple Excel Files into One using REST API in Python
client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_merger_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""
# API initialization
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
my_storage = ""
# Create download file request
request = groupdocs_merger_cloud.DownloadFileRequest("joined.xlsx", my_storage)
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# Create instance of the API
documentApi = groupdocs_merger_cloud.DocumentApi.from_keys(client_id, client_secret)
# Define first input file
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("sample1.xlsx")
# Define second input file
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("sample2.xlsx")
# Set sheet numbers range to merge
item2.start_page_number = 3
item2.end_page_number = 4
# Range mode
item2.range_mode = "OddPages"
# Define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "joined_sheets.xlsx"
# Create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# Join the files
result = documentApi.join(request)
# Done
print("Documents merged: " + result.path)
# Create instance of the API
documentApi = groupdocs_merger_cloud.DocumentApi.from_keys(client_id, client_secret)
# Define first input file
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("sample1.xlsx")
# Define second input file
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("sample2.xlsx")
# Define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "joined.xlsx"
# Create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# Join the files
result = documentApi.join(request)
# Done
print("Documents merged: " + result.path)
# Create an instance of the APIs
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
storage_api = groupdocs_merger_cloud.StorageApi.from_config(configuration)
# upload sample files
for filename in glob.iglob("C:\\Files\\upload\\*.xlsx", recursive=True):
destFile = filename.replace("C:\\Files\\upload", "", 1)
# check if file already exist
fileExistsResponse = storage_api.object_exists(groupdocs_merger_cloud.ObjectExistsRequest(destFile))
if not fileExistsResponse.exists:
# create upload file request
request = groupdocs_merger_cloud.UploadFileRequest(destFile, filename)
# upload file
file_api.upload_file(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment