Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created October 25, 2018 20:33
Show Gist options
  • Save mauricioaniche/9efb2e5e52fba9d3378b08f611e90b5e to your computer and use it in GitHub Desktop.
Save mauricioaniche/9efb2e5e52fba9d3378b08f611e90b5e to your computer and use it in GitHub Desktop.
Convert an Excel file to english
import os
from openpyxl import load_workbook
from google.cloud import translate
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/DIRECTORY-TO-YOUR-GOOGLE-KEY/google.json"
translate_client = translate.Client()
target = 'en'
wb = load_workbook(filename='/DIRECTORY/FILE-TO-TRANSLATE.xlsx', read_only=False)
count = 0
for sheet in wb.sheetnames:
ws = wb[sheet]
for row in ws.rows:
for cell in row:
if not cell.data_type == 's':
continue
to_translate = cell.value
if to_translate:
translation = translate_client.translate(to_translate, target_language=target)
cell.value = translation["translatedText"]
count = count + 1
print(count)
wb.save('/DIRECTORY/OUTPUT/RESULT.xlsx')
openpyxl
google-cloud-translate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment