Python implements word translation and python implements word translation.
Some unknown words often appear when reading English documents, but for some pdf readers, such as Foxit readers, You need to click (Extra-> Translate) to use the translation function ), if the translation function is enabled, you cannot use the annotation function, which is inconvenient for readers. To facilitate word query, you have developed a Python program in Windows to monitor and query words. The only operation is to select words and press Ctrl + C.
This Python program is mainly divided into three parts: Get the words to be queried from another application, Word Translation and word display.
Getting the words to be queried from another application involves inter-process communication. The easiest way to do this on Windows is to use the Clipboard function. There are multiple ways to use clipboard in Python, such as using win32clipboard (using clipboard in Python ). The clipboard implemented by Qt is used here. The clipboard Implementation of Qt supports callback when the system clipboard content changes. Specific implementation:
# Obtain the clipboard of Qt and bind the callback function self. clipboard = QApplication. clipboard () self. clipboard. dataChanged. connect (self. on_clipboard_changed)
# Callback function def on_clipboard_changed (self): data = self. clipboard. mimeData () if data. hasText (): word = data. text (). strip () m = re. match (R' [a-zA-Z] + ', word) if m: self. setWord (word) # self. setWindowFlags (self. windowFlags () & QtCore. qt. windowStaysOnTopHint) # self. setWindowState (self. windowState ()&~ QtCore. qt. windowMinimized | QtCore. qt. windowActive) trans = queryWords (word) self. setTrans (trans) ''' tip the window content has changed, but cannot move the window to the forground ''' self. activateWindow () else: print (word)
Word Translation directly calls youdao word translation. The link format of the word translation is http://dict.youdao.com/w/##/, in which the single word is used for searching. The obtained result is a single webpage that uses BeautifulSoup to extract the final translation result.
Def queryWords (word): ''' query the word ''' url = 'HTTP: // dict.youdao.com/w/##/'.format (word) html = getURL (url) soup = BeautifulSoup (html. text, 'html. parser ') trans_container = soup. find (class _ = 'Trans-container') if not trans_container: ''' not found the translation''' return [word] trans_li = trans_container.find_all ('lil ') trans_data = [li. text. strip () for li in trans_li] return trans_data
The last display is the interface written through Qt. After you want to query a word, the interface window is automatically activated and moved to the frontend. However, Windows does not allow you to move the window to the frontend without user operations, so the last step is to prompt the user through the activation window.
Complete code located in https://github.com/llf2017/pyDict/blob/master/pyDict.py
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.