切換到python.exe所在目錄。
python -m pip install googletrans==4.0.0-rc1 -i https://mirrors.aliyun.com/pypi/simple/
參閱博文https://plugin.blog.csdn.net/article/details/124514912
將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);
如有疑問,敬請留言。