简体   繁体   English

使用 python package "googletrans" 时出现翻译错误

[英]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我想修复下面的代码,它应该翻译位于 excel 中 A 列中名为“translation.xlsx”的单词,但它给了我一个错误
  2. Also it should give me the output/result(translated words) in the same excel(translation.xlsx") in column B.它还应该在 B 列的同一个 excel(translation.xlsx") 中给我输出/结果(翻译的单词)。

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.您正在尝试将 function translate()Translator class 本身一起使用,而不是它的一个实例。

Try:尝试:

gs = Translator()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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