Python 100 project #21: Automated Excel translate with Google Cloud Translate

I extended last project module, and made a CLI tool to translate the entire row into English.

 

Output Example:

These questions in Japanese becomes…

English.

 

Here is the code:

import openpyxl

import google_clouds

TARGET_COLUMN = 'C'

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

print('opening workbook')

workbook = sys.argv[1]

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

print('reading rows...')
for row in range(2, len(target[TARGET_COLUMN]) + 1):
    original_text = target[TARGET_COLUMN + str(row)].value
    if original_text is not None and len(original_text) > 0:
        target[TARGET_COLUMN + str(row)].value = google_clouds.get_translate(original_text)

wb.save('Translated_' + workbook.split('/')[-1])

print('done...')