1. win10安装python
我安装的是python3.12
2. 编辑翻译脚本
申请百度翻译api的key和appid
注意安装相关依赖包:requests win10toast
import requests
import random
import json
from hashlib import md5
import sys
import pyperclip
from win10toast import ToastNotifier
# Set your own appid/appkey.
appid = 'XXX'
appkey = 'xxx'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
#query = 'Hello World! This is 1st paragraph.\nThis is 2nd paragraph.'
#query = " %s" % " ".join(sys.argv[1:])
query = pyperclip.paste()
word =query.split()[0]
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()['trans_result'][0].get('dst')
print(result)
toaster = ToastNotifier()
toaster.show_toast("翻译结果:", result)
3. 配置xshell
配置xshell快捷键alt+f
,如下