寫好的程序可以配合https://www.cnblogs.com/jkn1234/p/9672957.html 進行打包
寫本程序使用到的參考資料
- google翻譯API https://blog.csdn.net/roslei/article/details/72911390
- 利用python調用谷歌翻譯API https://www.jianshu.com/p/95cf6e73d6ee
- Python中使用多個分隔符分隔字符串re.split https://blog.csdn.net/programmer_at/article/details/77409507
- python3 使用pyperclip讀寫剪貼板(windows) https://www.cnblogs.com/gayhub/p/5491801.html
沒有使用到的參考資料
- 此頁面顯示如何安裝Google Cloud SDK,初始化它,以及從命令行運行核心gcloud命令。 https://cloud.google.com/sdk/docs/quickstart-windows
- python 調用Google Translate API 翻譯 https://blog.csdn.net/u010856630/article/details/73810718
- Python 全局熱鍵功能(多個) https://blog.csdn.net/lsjweiyi/article/details/79137931
- Python 鍵盤鼠標監聽 https://blog.csdn.net/Marksinoberg/article/details/51754592
- pyhook模塊的安裝 https://stackoverflow.com/questions/35202087/pyhook-on-python-3-5
''' 這是一個自動翻譯剪切板內容的程序 它可以讀取剪切板內容,然后分句多次進行谷歌翻譯,最后將翻譯的結果寫會剪切板 可以配合autohotkey使用,按下^t時復制選中文本,同時啟動翻譯剪切板程序 ''' import json import execjs # 需要先用pip install PyExecJS安裝,用來執行js腳本 import requests import pyperclip import re import copy # 這個類提供了getTk函數 # 功能是根據待翻譯的內容計算相應的Google翻譯api里面的tk參數。 # tk參數是浮點數 class Py4Js(): def __init__(self): self.ctx = execjs.compile(""" function TL(a) { var k = ""; var b = 406644; var b1 = 3293161072; var jd = "."; var $b = "+-a^+6"; var Zb = "+-3^+b+-f"; for (var e = [], f = 0, g = 0; g < a.length; g++) { var m = a.charCodeAt(g); 128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128) } a = b; for (f = 0; f < e.length; f++) a += e[f], a = RL(a, $b); a = RL(a, Zb); a ^= b1 || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + jd + (a ^ b) }; function RL(a, b) { var t = "a"; var Yb = "+"; for (var c = 0; c < b.length - 2; c += 3) { var d = b.charAt(c + 2), d = d >= t ? d.charCodeAt(0) - 87 : Number(d), d = b.charAt(c + 1) == Yb ? a >>> d: a << d; a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d } return a } """) def getTk(self, text): return self.ctx.call("TL", text) # 構建Google翻譯api的函數。主要是tk參數和q參數 # text是待翻譯的內容 # tk是根據待翻譯的內容生成的一個浮點數作為api里面的tk參數 def buildUrl(text, tk): baseUrl = 'https://translate.google.cn/translate_a/single' baseUrl += '?client=t&' baseUrl += 's1=auto&' baseUrl += 't1=zh-CN&' baseUrl += 'h1=zh-CN&' baseUrl += 'dt=at&' baseUrl += 'dt=bd&' baseUrl += 'dt=ex&' baseUrl += 'dt=ld&' baseUrl += 'dt=md&' baseUrl += 'dt=qca&' baseUrl += 'dt=rw&' baseUrl += 'dt=rm&' baseUrl += 'dt=ss&' baseUrl += 'dt=t&' baseUrl += 'ie=UTF-8&' baseUrl += 'oe=UTF-8&' baseUrl += 'otf=1&' baseUrl += 'pc=1&' baseUrl += 'ssel=0&' baseUrl += 'tsel=0&' baseUrl += 'kc=2&' baseUrl += 'tk=' + str(tk) + '&' # url中由%數字替換一些特殊的字符 text = text.replace('+', '%2B') text = text.replace('&', '%26') text = text.replace('#', '%23') baseUrl += 'q=' + text return baseUrl # 將text里面的文本翻譯成中文,並返回 # text里面的語言是自動檢測的,可以是英語、日語、德語等任何語言 def translate(text): header = { 'authority': 'translate.google.cn', 'method': 'GET', 'path': '', 'scheme': 'https', 'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'cookie': '', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36', 'x-client-data': 'CIa2yQEIpbbJAQjBtskBCPqcygEIqZ3KAQioo8oBGJGjygE=' } url = buildUrl(text, js.getTk(text)) res = '' try: r = requests.get(url) result = json.loads(r.text) if result[7] != None: # 如果我們文本輸錯,提示你是不是要找xxx的話,那么重新把xxx正確的翻譯之后返回 ? try: correctText = result[7][0].replace('<b><i>', ' ').replace('</i></b>', '') print(correctText) correctUrl = buildUrl(correctText, js.getTk(correctText)) correctR = requests.get(correctUrl) newResult = json.loads(correctR.text) res = newResult[0][0][0] except Exception as e: print(e) res = result[0][0][0] else: i = 0 while result[0][i][0] != None: res += result[0][i][0] i += 1 except Exception as e: res = '' print(url) print("翻譯" + text + "失敗") print("錯誤信息:") print(e) finally: return res if __name__ == '__main__': t0 = str(pyperclip.paste()) # t0存放剪切板的內容 # 將所有的.;?全部替換為英文的','防止一行的句子翻譯到英文句子結束符號時就不翻譯了. # 注意最好不要替換為中文符號, # 因為谷歌翻譯時會用英文的標點符號進行斷句,中文的符號起不到這個作用 t0 = t0.replace('.', ',') t0 = t0.replace(';', ',') t0 = t0.replace('?', ',') t0 = t0.split("\r\n")# 按行分割句子 # 翻譯t5中的每一個句子,將翻譯好的句子用'\r\n'連接后放入combine combine = '' js = Py4Js() for i in t0: temp = copy.deepcopy(i) res = translate(temp) # SendInput and SendPlay combine = combine + res + '\r\n' print(combine) pyperclip.copy(combine)# 將翻譯好的結果放入剪切板 # js = Py4Js() # print("Yes, Removing pyinstaller and reinstalling with pip install git add git://github.com/pyinstaller/pyinstaller@develop as well as including PyQt5.sip in my hiddenimports fixed the issue.") # SendInput and SendPlay # res = translate("Quora is a simple question-and-answer site。 Whatever your question, type it in the search box and, if there isn't already an answer there, users will pile in and attempt to answer it。 Information is organised more like Wikipedia than Google, with answers prioritised by how useful they are, but the site uses Twitter-style following to track the best contributors.") # SendInput and SendPlay # print(res)