from googletrans import Translator
# 加载中文txt文件
with open('data.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 创建Translator对象
translator = Translator(service_urls=['translate.google.com'])
# 将中文文本翻译为西班牙语
# translated = translator.translate(text, src='zh-cn', dest='es').text
translated = translator.translate(text, src='zh-cn', dest='en').text
# 将翻译结果保存到新的txt文件中
with open('output.txt', 'w', encoding='utf-8') as f:
f.write(translated)
print("翻译完成")
from googletrans import Translator
最新推荐文章于 2024-04-16 22:20:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)