
How to Combine Multiple Word Documents using Python
Combine two or more word documents into a single word file programmatically on the cloud using REST API. Our online docx merger API provides a convenient solution to combine and merge multiple word documents into one word document online instead of processing files one by one. As a Python developer, you can merge two word documents online into a single word document. In this article, you will learn how to combine multiple word documents using Python.
The following topics shall be covered in this article:
- Word Documents Merger REST API – Python SDK
- How to Combine Word Files in Python using REST API
- Merge Specific Pages of Multiple Word Files in Python
Word Documents Merger REST API – Python SDK
To merge word files online, I will be using the Python SDK of GroupDocs.Merger Cloud API. It supports to combine two or more files into a single word document or extract a document pages from single document. Word merge online also allows you to move, delete, exchange, rotate or change the pages orientation either as portrait or landscape for the whole or preferred range of pages. This SDK supports merging and splitting of all popular document formats such as Word, Excel, PowerPoint, Visio, OneNote, PDF, HTML, etc.
Install GroupDocs.Merger Python SDK to merge docx files online using the below command:
pip install groupdocs_merger_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:
# Import Python SDK in your python application from http://api.groupdocs.cloud | |
import groupdocs_merger_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 File API configurations | |
configuration = groupdocs_merger_cloud.Configuration(client_id, client_secret) | |
configuration.api_base_url = "https://api.groupdocs.cloud" | |
storage_name = "MyStorage" |
How to Combine Word Files in Python using REST API
For merging word documents online on the cloud, we will be following the simple steps mentioned below:
Upload the Word Files
Firstly, upload the word files to the cloud using the code example given below:
# Upload Word document to cloud storage | |
# Create instance of the API | |
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("H:\\groupdocs-cloud-data\\upload\\*.docx", recursive=True): | |
destFile = filename.replace("H:\\groupdocs-cloud-data\\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 to the cloud | |
response = file_api.upload_file(request) | |
print(response.uploaded) |
As a result, the uploaded word files will be available in the files section of your dashboard on the cloud.
Merge Multiple Word Files using Python
Combine word files online into a single file programmatically by following the steps mentioned below:
- Create an instance of the DocumentApi
- Create an instance of the JoinItem
- Provide the input file path for first JoinItem in the FileInfo
- Create another instance of the JoinItem
- Provide the input file path for second JoinItem in the FileInfo
- Add more JoinItems for merging more than two files
- Create an instance of the JoinOptions
- Add a comma separated list of created join items
- Set the output file path
- Create an instance of the JoinRequest with JoinOptions
- Finally, combine files by calling the join() method of the DocumentAPI with JoinRequest
The following code snippet shows how to combine word documents into one online using Python:
# How to merge Word Docx files in Python | |
# Create necessary API instances | |
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration) | |
# Input source file 1 | |
item1 = groupdocs_merger_cloud.JoinItem() | |
item1.file_info = groupdocs_merger_cloud.FileInfo("python-testing/word-one.docx") | |
# Input source file 2 | |
item2 = groupdocs_merger_cloud.JoinItem() | |
item2.file_info = groupdocs_merger_cloud.FileInfo("python-testing/word-two.docx") | |
# Define join options | |
options = groupdocs_merger_cloud.JoinOptions() | |
options.join_items = [item1, item2] | |
options.output_path = "python-testing/joined-files.docx" | |
# Create join request | |
request = groupdocs_merger_cloud.JoinRequest(options) | |
# Merge docx files | |
result = documentApi.join(request) | |
print("Successfully merged Word Docx files: " + str(result)) |
Download the Merged File
Now you know how to combine word documents and keep formatting using python. You can download it using the following code sample:
# API initialization to download converted file | |
import shutil | |
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration) | |
# Create download file request | |
request = groupdocs_merger_cloud.DownloadFileRequest("python-testing\\joined-files.docx", 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\\") |
Merge Specific Pages of Multiple Word Files in Python
Next, combine specific pages of multiple word files into a single document programmatically by following the steps mentioned below:
- Create an instance of the DocumentApi
- Create an instance of the JoinItem
- Provide the input file path for first JoinItem in the FileInfo
- Define a list of page numbers in a comma separated array
- Create another instance of the JoinItem
- Provide the input file path for second JoinItem in the FileInfo
- Define start page number and end page number
- Define the page range mode as OddPages
- Create an instance of the JoinOptions
- Add a comma separated list of created join items
- Set the output file path
- Create an instance of the JoinRequest with JoinOptions
- Finally, merge word files by calling the join() method of the DocumentAPI with JoinRequest
The following code snippet shows how to merge specific pages of word files into one using REST API in Python:
# How to merge specific pages of multiple docx files in Python | |
# Create and initialize api instances | |
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration) | |
# Input source file 1 | |
item1 = groupdocs_merger_cloud.JoinItem() | |
item1.file_info = groupdocs_merger_cloud.FileInfo("python-testing/word-one.docx") | |
# page numbers of specific pages to merge | |
item1.pages = [1,2] | |
# Input source file 2 | |
item2 = groupdocs_merger_cloud.JoinItem() | |
item2.file_info = groupdocs_merger_cloud.FileInfo("python-testing/word-two.docx") | |
# start page number | |
item2.start_page_number = 2 | |
# end page number | |
item2.end_page_number = 4 | |
# pages range mode | |
item2.range_mode = "OddPages" | |
# Define join options | |
options = groupdocs_merger_cloud.JoinOptions() | |
options.join_items = [item1, item2] | |
options.output_path = "python-testing/joined-pages.docx" | |
# Create join request | |
request = groupdocs_merger_cloud.JoinRequest(options) | |
# Merge docx files | |
result = documentApi.join(request) | |
print("Successfully merged Word pages: " + str(result)) |
Merge Word Documents Free Online
How to combine word documents online free? Please try the following to merge documents online for free, which is developed using the above API.
Conclusion
In this tutorial, we have learned:
- how to combine word documents online on the cloud using python;
- how to programmatically upload and download the merged docs file;
- join pages of multiple word documents online into single file in Python;
Additionally, you can learn more about GroupDocs.Merger Cloud API using the documentation. We also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Moreover, please see the GroupDocs.Merger Cloud SDK for Python Examples here.
Ask a question
If you have any questions about word document merger, please feel free to ask us on the Free Support Forum.