Translate your notes with Azure Translator and Python
Published Mar 28 2022 06:04 AM 1,350 Views
Senior Member



Welcome to part 2 of the “Digitize and translate your notes with Azure Cognitive Services and Python” series. In the previous blog post, you explored the pre-trained models of Azure Computer Vision for Optical Character Recognition and extracted handwritten text from an image using the READ API and the Computer Vision SDK for Python.


About me

Hi, I am Foteini Savvidou, a Gold Microsoft Learn Student Ambassador!




I am an undergraduate Electrical and Computer Engineering student at Aristotle University of Thessaloniki (Greece) interested in AI, cloud technologies and biomedical engineering. Always passionate about teaching and learning new things, I love helping people expand their technical skills through organizing workshops and sharing articles on my blog.



In this article, you will translate the extracted text from your handwritten notes using Translator. You will:

  • Understand how the Azure Translator works.
  • Create a Python script to translate text.

To complete the exercise, you will need to install:

  • Python 3, and
  • Visual Studio Code.

You will also need an Azure subscription. If you don’t have one, you can sign up for an Azure free account. If you are a student, you can apply for an Azure for Students Subscription.


What is Translator?

Translator is a cloud-based service that is part of the Azure Cognitive Services and enables you to perform language translation and other language-related operations.

Study the following sketch note to learn more about the capabilities of the Translator service.




You can find more information and how-to-guides about Translator on Microsoft Learn and Microsoft Docs.


Translate text with Azure Translator and Python


Create a configuration file

Create a configuration file and add the authentication key for your Cognitive Services resource, and the location in which the service is deployed. You can copy both values from the Keys and Endpoint page of your Cognitive Services resource.




Create a new Python script

Create a new Python script, for example and open it in Visual Studio Code or in your preferred editor.

Want to view the whole code at once? You can find it on GitHub.

  1. Import the following llibraries.
    from dotenv import load_dotenv
    import os
    import requests


  2. Add the following code to load the Cognitive Services key and region from the configuration file and specify the endpoint.
    key = os.getenv('COG_SERVICE_KEY')
    region = os.getenv('COG_SERVICE_REGION')
    endpoint = ''


Detect language

Define the detect_language function which automatically detects the language of the source text and returns the language code.


def detect_language(text, key, region, endpoint):
    # Use the Translator detect function
    path = '/detect'
    url = endpoint + path
    # Build the request
    params = {
        'api-version': '3.0'
    headers = {
    'Ocp-Apim-Subscription-Key': key,
    'Ocp-Apim-Subscription-Region': region,
    'Content-type': 'application'
    body = [{
        'text': text
    # Send the request and get response
    request =, params=params, headers=headers, json=body)
    response = request.json()
    # Get language
    language = response[0]["language"]
    # Return the language
    return language



Translate text

Define the translate function which translates the given text from the source_language into the target_language and returns the translated text.


def translate(text, source_language, target_language, key, region, endpoint):
    # Use the Translator translate function
    url = endpoint + '/translate'
    # Build the request
    params = {
        'api-version': '3.0',
        'from': source_language,
        'to': target_language
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': region,
        'Content-type': 'application'
    body = [{
        'text': text
    # Send the request and get response
    request =, params=params, headers=headers, json=body)
    response = request.json()
    # Get translation
    translation = response[0]["translations"][0]["text"]
    # Return the translation
    return translation



If you don't include the from parameter in your translation request, the Translator will attempt to detect the language of the given text.

Add the following code and run the script:


print(detect_language('Hello world!', key, region, endpoint))
print(translate('Hello world!', 'en', 'el', key, region, endpoint))



Challenge: Modify the translate function so that it returns the translation of the given text into two or more language.
If you need some help, you can check the script in my GitHub repo.


Extract and translate text from handwritten notes

In this section, you will build a Python app that uses the READ API to extract handwritten text from notes and then translates the extracted text using the Translator service.

First download the images used in the following examples from my GitHub repository.

Next, create a new Python script, for example and use the get_text function from the previous post and the detect_language and translate functions you wrote previously to extract and translate the text of the given images.

Add the following lines of code in the main function to test your app:

# Analyze each image file in the images folder
images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images")
for file_name in os.listdir(images_folder):
    # Extract text
    print("\n" + "="*12)
    read_image_path = os.path.join (images_folder, file_name)
    text = get_text(read_image_path, computervision_client)
    # Detect the language
    language = detect_language(text, key, region, endpoint)
    print("Language:", language)
    # Translate
    target_lang = ["el"]
    results = translate(text, language, target_lang, key, region, endpoint)
    for i in range(len(results)):
        print("\n" + "-"*12)
        print(target_lang[i] + ":", results[i])



Summary and next steps

In this article, you learned how to use Azure Translator to detect the language of a given text and translate text from the source language into one or more languages. Then you built a Python app that uses the Azure Computer Vision READ API to extract text from handwritten notes and the Translator service to translate the extracted text.

If you are interested in learning more about Azure Translator, check out these Microsoft Learn modules:

In the next articles, you will learn how to build and deploy a Flask AI web app that uses the READ API and the Azure Translator.



If you have finished learning, you can delete the resource group from your Azure subscription:

  1. In the Azure portal, select Resource groups on the right menu and then select the resource group that you have created.
  2. Click Delete resource group.
Version history
Last update:
‎Mar 26 2022 09:48 AM
Updated by: