Link Search Menu Expand Document

Multiple Text Replacements From URL - Python

PDF Search and Replace Text sample in Python demonstrating ‘Multiple Text Replacements From URL’
import os
import requests 
import json

API_KEY = "******************************"
SourceFileURL = ""
Password = ""
DestinationFile = ".\\result.pdf"

def main(args = None):
    replaceStringFromPdf(SourceFileURL, DestinationFile)

search_strings = ["Your Company Name", "Client Name", "Item"]
replace_strings = ["XYZ LLC", "ACME", "SKU"]

def replaceStringFromPdf(uploadedFileUrl, destinationFile):
    """Replace Text from PDF using Web API"""

    # Prepare requests params as JSON
    # See documentation:
    parameters = {
        "name": os.path.basename(destinationFile),
        "password": Password,
        "url": uploadedFileUrl,
        "searchStrings": search_strings,
        "replaceStrings": replace_strings,
        "replacementLimit": 1

    # Prepare URL for 'Replace Text from PDF' API request
    url = "{}/pdf/edit/replace-text".format(BASE_URL)

    # Convert parameters dictionary to JSON string
    payload = json.dumps(parameters)

    # Execute request and get response as JSON
    response =, data=payload, headers={ "x-api-key": API_KEY })

    if (response.status_code == 200):
        json_data = response.json()

        if json_data["error"] == False:
            #  Get URL of result file
            resultFileUrl = json_data["url"]            
            # Download result file
            r = requests.get(resultFileUrl, stream=True)
            if (r.status_code == 200):
                with open(destinationFile, 'wb') as file:
                    for chunk in r:
                print(f"Result file saved as \"{destinationFile}\" file.")
                print(f"Request error: {response.status_code} {response.reason}")
            # Show service reported error
        print(f"Request error: {response.status_code} {response.reason}")

if __name__ == '__main__':
    main() Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

Get your API key here!

Download Source Code (.zip)

return to the previous page explore PDF Search and Replace Text endpoint