简体   繁体   中英

Translation error while using python package "googletrans"

  1. I want to fix the below code, it should translate the words located in column A in the excel called "translation.xlsx", but it gives me an error
  2. Also it should give me the output/result(translated words) in the same excel(translation.xlsx") in column B.

code is here

import openpyxl
from googletrans import Translator

loc = r"C:\Users\userid\Desktop\translation.xlsx"

gs = Translator.translate()

wb = openpyxl.load_workbook(loc)
sheet = wb.active

for i in range(2, sheet.max_row + 1):
    original = sheet.cell(row=i, column=1).value
    translated = gs.translate(original, 'de')
    sheet.cell(row=i, column=2).value = translated

wb.save(loc)

Error:

Traceback (most recent call last):
  File "c:\Users\userid\translation.py", line 8, in <module>
    gs = Translator.translate()
TypeError: translate() missing 2 required positional arguments: 'self' and 'text'

You're trying to use the function translate() with the Translator class itself, not an instance of it.

Try:

gs = Translator()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM