【问题标题】:Excel sheet translation using python使用python翻译Excel工作表
【发布时间】:2020-02-09 12:35:39
【问题描述】:

我有一个充满英文文本的 excel 表,所以现在我想将每个文本转换为其他语言(例如:法语)并使用 python 插入到 excel 表的下一列中。

那么如何处理呢?

我目前正在浏览 python 的 pandas。

【问题讨论】:

    标签: python excel pandas language-translation google-translation-api


    【解决方案1】:

    这应该可行。确保你pip install googletrans-temp

    import pandas as pd
    from googletrans import Translator 
    
    # read from an excel file
    df = pd.read_excel('/Users/andrewgreatorex/Downloads/test_spreadsheet.xlsx')
    
    # translate a column to French, and add back into the DataFrame
    translator = Translator()         
    df['French'] = df['text'].apply(translator.translate,src='en',dest='fr').apply(getattr, args=('text',))
    
    # output new excel file
    df.to_excel('name_of_your_output_file.xlsx')
    

    【讨论】:

    • 谢谢安迪!!!但有时它工作正常,有时我得到“JSONDecodeError:期望值”我可以知道它的原因。如果我收到此错误,则它根本无法正常工作。
    • df['French'] = df['text'].apply(translator.translate,src='en',dest='fr').apply(getattr, args=('text ',))..... 如果一旦出现,则代码根本不会运行并抛出相同的错误
    • 知道这个错误是因为谷歌翻译有15k的字符限制。如何克服这一点?我应该更改代码吗?
    • 尝试使用不同的翻译包,pypi.org/project/translate 看起来可以解决问题
    • 是的,它有效!但即使这样也有与谷歌翻译 API 相同的翻译限制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2021-01-11
    • 1970-01-01
    • 2020-11-29
    • 2018-04-15
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多