簡體   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