Created
October 25, 2018 20:33
-
-
Save mauricioaniche/9efb2e5e52fba9d3378b08f611e90b5e to your computer and use it in GitHub Desktop.
Convert an Excel file to english
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openpyxl | |
google-cloud-translate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment