Python 100 project #22: Automated Excel translate with Multiple Translation Choice

This is a extended version from the previous project. This time, I created Microsoft Azure Text Translate version of translation module. And user can now select the translation either from Google or MS.


Output Example:

$ python3 data_source/questionsTest.xlsx 
opening workbook
reading rows...
translating ... アプリケーションのスタート方法がわからない
first candidate ... I do not know how to start the application
>>>Please select one from ['Y', 'N']: Y
translating ... 赤丸の挿入方法を教えて欲しい
first candidate ... Please tell me how to insert red circle
>>>Please select one from ['Y', 'N']: N
second candidate ... How do i insert a red circle?
>>>Please select one from ['Y', 'N']: Y
translating ... 文章の削除の方法はどうしたらいいか
first candidate ... How can I delete sentences?
>>>Please select one from ['Y', 'N']: N
second candidate ... How do I delete sentences?
>>>Please select one from ['Y', 'N']: N
Please select the option:
0 - keep the original sentence
1 - use the first candidate
2 - use the second candidate
>>>Please select one from [0, 1, 2]: 0


Here is the code:

def get_translate(sentence, lang='en'):
    import http.client, json

    from data_source.ms_credentials import get_credential

    host = ''
    path = '/translate?api-version=3.0'
    params = "&to=" + lang
    headers = get_credential()

    requestBody = [{
        'Text': sentence,
    content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')

    conn = http.client.HTTPSConnection(host)
    conn.request("POST", path + params, content, headers)
    response = conn.getresponse ()

    response_text = json.loads([0]['translations'][0]['text']

    return response_text
import sys

import openpyxl

import google_clouds
import ms_azure


if len(sys.argv) != 2:
    print(f"Usage: {sys.argv[0]} 'original excel file'")

print('opening workbook')

workbook = sys.argv[1]

wb = openpyxl.load_workbook(workbook)
sheets = wb.sheetnames
target = wb[sheets[0]]
# target = wb.copy_worksheet(original)

def ask_selection(selection):
    while True:
        user_input = input(f'>>>Please select one from {selection}: ')
            user_input = int(user_input)
        if user_input in selection:
            return user_input

print('reading rows...')
for row in range(2, len(target[TARGET_COLUMN]) + 1):
    translations = []
    original_text = target[TARGET_COLUMN + str(row)].value
    if original_text is not None and len(original_text) > 0:
        print(f'translating ... {original_text}')
        google_translation = google_clouds.get_translate(original_text)
        print(f"first candidate ... {google_translation}")

        if ask_selection(['Y', 'N']) == 'Y':
            selected_translation = 1
            ms_translation = ms_azure.get_translate(original_text)
            print(f"second candidate ... {ms_translation}")

            if ask_selection(['Y', 'N']) == 'Y':
                selected_translation = 2
                print('Please select the option:\n'
                      '0 - keep the original sentence\n'
                      '1 - use the first candidate\n'
                      '2 - use the second candidate')
                selected_translation = ask_selection(list(range(len(translations))))

        target[TARGET_COLUMN + str(row)].value = translations[selected_translation]'Translated_' + workbook.split('/')[-1])