文章目录
1.安装翻译库
- 1.安装翻译库
- 2.读取文件数据
- 3.操作步骤
- 4.作者答疑
切换到python.exe所在目录。
python -m pip install googletrans==4.0.0-rc1 -i https://mirrors.aliyun.com/pypi/simple/
2.读取文件数据
参阅博文https://plugin.blog.csdn.net/article/details/124514912
3.操作步骤将Html拷贝入txt文档,Python读取数据,调用谷歌翻译引擎,最后保存到文件。注意字符串长度不能超过5000字。
from googletrans import Translator
# 设置Google翻译服务地址
translator = Translator(service_urls=[
'translate.google.cn'
])
def ReadFile(filename):
ff=None;
with open(filename, 'r') as f:
ff = f.read();
return ff;
def WriteFile(filename,data):
# 打开一个文件
fo = open(filename, "w");
fo.write(data);
# 关闭打开的文件
fo.close();
htmlStr=ReadFile(r"C:\Users\ajz\Desktop\html.txt");
translation = translator.translate(htmlStr, dest='zh-CN');
print(translation.text);
WriteFile(r"C:\Users\ajz\Desktop\htmlrlt.txt",translation.text);
4.作者答疑
如有疑问,敬请留言。