繁体   English   中英

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

[英]Translation error while using python package "googletrans"

  1. 我想修复下面的代码,它应该翻译位于 excel 中 A 列中名为“translation.xlsx”的单词,但它给了我一个错误
  2. 它还应该在 B 列的同一个 excel(translation.xlsx") 中给我输出/结果(翻译的单词)。

代码在这里

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)

错误:

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'

您正在尝试将 function translate()Translator class 本身一起使用,而不是它的一个实例。

尝试:

gs = Translator()

暂无
暂无

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

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