Merge - Python

This tutorial shows how to merge documents using the LEADTOOLS Cloud Services in a Python application.

Overview  
Summary This tutorial covers how to make Merge requests and process the results using the LEADTOOLS Cloud Services in a Python application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform LEADTOOLS Cloud Services API
IDE Visual Studio
Language Python
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Be sure to review the following sites for information about LEADTOOLS Cloud Services API.

Application ID and Password

Create an Account with LEADTOOLS Hosted Cloud Services to obtain both Application ID and Password strings.

Service Plans

LEADTOOLS Service Plan offerings:

Service Plan Description
Free Trial Free Evaluation
Page Packages Prepaid Page Packs
Subscriptions Prepaid Monthly Processed Pages

To further explore the offerings, refer to the LEADTOOLS Hosted Cloud Services page.

To obtain the necessary Application ID and Application Password, refer to Create an Account and Application with the LEADTOOLS Hosted Cloud Services.

Add the Merge Code

With the project created and the requests package added, coding can begin.

In the Solution Explorer, open Merge.py. Add the following variables at the top.

Note

Where it states Replace with Application ID and Replace with Application Password, be sure to place your Application ID and Password accordingly.

# Simple script to showcasing how to use the Merge API in the LEADTOOLS Cloud Services. 
import requests 
import sys 
import time 
import json 
from enum import Enum 
 
servicesUrl = "https://azure.leadtools.com/api/" 
 
# The application ID. 
appId = "Replace with Application ID" 
 
# The application password. 
password = "Replace with Application Password" 
 
# Conversion output formats. 
class FormatsEnum(Enum): 
    Png = 1 
    Jpeg = 2 
    Tiff = 3 
    Pdf = 4 
    Pdfa = 5 
    PdfImage = 6 
    PdfImageOverText = 7 
    PdfaImageOverText = 8 
    Docx = 9 
    DocxFramed = 10 
    Rtf = 11 
    RtfFramed = 12 
    Txt = 13 
    TxtFramed = 14 

In order to facilitate the Merge request, create the following functions: def upload_url_for_merge(fileUrl) - This submits a file at a Url to the server and returns a GUID. def upload_file_for_merge(path) - This submits a local file to the server and returns a GUID. def check_file_verification(id) - This will validate the file was submitted to the server via a provided GUID.

# Function to upload a file from a URL to the LEADTOOLS Cloud Services to be used with the Merge API command. 
def upload_url_for_merge(fileUrl): 
    baseUploadUrl = '{}UploadFile?fileurl={}&forMerge=true' 
    formattedUploadUrl = baseUploadUrl.format(servicesUrl, fileUrl) 
    request = requests.post(formattedUploadUrl, auth=(appId, password)) 
 
    if request.status_code != 200: 
        print("Error sending the upload request for file: " + fileUrl) 
        print(request.text) 
        sys.exit() 
 
    return request.text 
 
# Function to upload a file to the LEADTOOLS Cloud Services to be used with the Merge API command. 
def upload_file_for_merge(path): 
    baseUploadUrl ='{}UploadFile&forMerge=true' 
    formattedUploadUrl = baseUploadUrl.format(servicesUrl) 
    file = {'file' : open(path, 'rb')} 
    request = requests.post(formattedUploadUrl, auth=(appId, password), files = file) 
 
    if request.status_code != 200: 
        print("Error sending the upload request for file: " + path) 
        print(request.text) 
        sys.exit() 
 
    return request.text 
 
# Function to check if a file has passed the verification process. 
def check_file_verification(id): 
    baseQueryUrl = '{}Query?id={}' 
    formattedQueryUrl = baseQueryUrl.format(servicesUrl, id) 
    returnedData = "" 
    while True: 
        request = requests.post(formattedQueryUrl, auth=(appId, password)) 
        returnedData = request.json() 
        # We will check to make sure that the file has passed verification.  For the full list of possible File Statuses, refer to our documentation here: 
        # https://services.leadtools.com/documentation/api-reference/query 
        if returnedData['FileStatus'] != 123: 
            break 
        time.sleep(5) 
 
    if returnedData['FileStatus'] != 122: 
        print("The file has failed the verification process with File Status: " + returnedData['FileStatus']) 
        return False 
 
    return True 

With the functions created, they can now be called and the files submitted and verified.

# We will be uploading the file via a URl.  Files can also be passed by adding a PostFile to the request.  Only 1 file will be accepted per request. 
# The Merge Api requires you to upload your files with the forMerge parameter set.  A file that has been uploaded and tagged as forMerge is not compatible with other LEADTOOLS Cloud Services API functions. 
tiffFileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif' 
firstFileId = upload_url_for_merge(tiffFileURL) 
print("First File ID: " + firstFileId) 
 
# At this point the file has been successfully uploaded to the LEADTOOLS Cloud Services and is undergoing verification.  Before we continue, we need to make sure that the uploaded file has passed verification 
if check_file_verification(firstFileId) == False: 
    sys.exit() 
 
# We will now upload the second file we would like to merge 
pdfFileUrl = 'https://demo.leadtools.com/images/pdf/leadtools.pdf' 
secondFileId = upload_url_for_merge(pdfFileUrl) 
 
print("Second File ID: " + secondFileId) 
if check_file_verification(secondFileId) == False: 
    sys.exit() 

Once complete gather GUIDs from the two submitted files and other merge information, then process the Merge request. This sends an Merge request to the LEADTOOLS Cloud Services API, if successful, a unique identifier (GUID) will be returned and then a query using this GUID will be made.

# We now need to create the JSON body response for the Merge Api Command 
data = [] 
 
firstFileData = {} 
# pass every page in the file 
firstFileData['firstPage'] = 1 
firstFileData['lastPage'] = -1 
firstFileData['fileId'] = firstFileId 
data.append(firstFileData) 
 
secondFileData = {} 
# Make sure this file's pages are merged in a specific order. 
secondFileData['pages'] = [5,1,2,4,3] 
secondFileData['fileId'] = secondFileId 
data.append(secondFileData) 
 
# The output format of the merged files 
mergeFormat = FormatsEnum.Pdf.value 
 
baseMergeUrl = '{}Conversion/Merge?format={}' 
formattedMergeUrl = baseMergeUrl.format(servicesUrl, mergeFormat) 
request = requests.post(formattedMergeUrl, auth=(appId, password), json=data) 
if request.status_code != 200: 
        print("Error sending merge request") 
        print(request.text) 
        sys.exit() 

Next, create a Query request that utilizes the GUID provided by Merge request. If successful, the response will contain all the request data in JSON format.

# At this point the merge request has been successfully received by the server, and is being processed. 
baseQueryUrl = '{}Query?id={}' 
formattedQueryUrl = baseQueryUrl.format(servicesUrl, firstFileId) 
 
# Poll the services to determine if the request has finished processing 
while True: 
    request = requests.post(formattedQueryUrl, auth=(appId, password)) 
    returnedData = request.json() 
    if returnedData['FileStatus'] != 100: 
        break 
    time.sleep(5) 
 
print("File finished processing with file status: " + 
    str(returnedData['FileStatus'])) 
 
if returnedData['FileStatus'] != 200: 
    sys.exit() 

Finally, parse the JSON data into a readable format.

try: 
    print("Results:") 
    returnedJson = returnedData['RequestData'] 
    for requestObject in returnedJson: 
        print("Service Type: " + requestObject['ServiceType']) 
        if requestObject['ServiceType'] == 'Conversion': 
            print("Returned URLS:") 
            for url in requestObject['urls']: 
                print(url) 
except Exception as e: 
    print("Failed to Parse JSON") 
    print(e) 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application displays the link to the merged file from the returned JSON data.

Merged File Information

Wrap-up

This tutorial showed how to merge files via the LEADTOOLS Cloud Services API.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.