Python implements word translation and python implements word translation.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.