Search for answers in the Test database developed by applet

Source: Internet
Author: User
I'm bored when I finish the final exam. CY then came to my bedroom and reminded me that I could develop a program to automatically find the answer in excel. after giving his ideas, he started work immediately. The question Library with the characteristics of our school is to download an excel worksheet. the assignments and questions are all stored on the webpage. we need to find answers in excel. After taking this process into consideration, the program mainly implements the following functions: after the final exam is completed, you are bored and are brushing the question bank. CY then came to my bedroom and reminded me that I could develop a program to automatically find the answer in excel. after giving his ideas, he started work immediately.

The question Library with the characteristics of our school is to download an excel worksheet. the assignments and questions are all stored on the webpage. we need to find answers in excel. After taking this process into consideration, the program mainly implements the following functions:

1. monitors clipboard changes;

2. if the clipboard content changes, use the regular expression to find the first most matched question in the question library based on the content in the clipboard;

3. output the answer on the console.

Considering that python has rich libraries, it is not a hassle to connect to an excel file or monitor the clipboard. Therefore, python is used for development.

Xlrd is used to read excel files and pythonwin is used to monitor the clipboard.

def getText():     w.OpenClipboard()     d = w.GetClipboardData(win32con.CF_TEXT)     w.CloseClipboard()     return d  def setText(aString):    w.OpenClipboard()    w.EmptyClipboard()    w.SetClipboardData(win32con.CF_TEXT, aString)    w.CloseClipboard()

Functions searched on the Internet to obtain the clipboard content and set the clipboard content.

Read the excel file on the desktop, and Du Niang obtained the desktop location through the registry.

= _winreg.QueryValueEx(key, )[0]

After that, open the. xls file through the hosts in the xlrddatabase and select sheet.

    xlsfile = xlrd.open_workbook(get_desktop()+'\\exercise.xls')    mysheet = xlsfile.sheet_by_name('1')

Then you need to monitor the clipboard. I won't do anything like listeners, so I will check whether the clipboard content changes every 1 s.

if(q != getText()):            q=getText()        else:            time.sleep(1)            continue

After obtaining the content in the clipboard, use a regular expression to match the question. if the match is successful, the answer is output. The encoding is involved here, and I don't know why it is written like this. it is actually implemented...

 row (re.search(+q.decode()+, mysheet.cell(row,0).value)!== 1                 +mysheet.cell(row,77= mysheet.cell(row,7

Why setText (mysheet. cell (row, 7 ). value) NA, because the mouse cannot leave the browser during the exam, it is not good to put cmd clearly on the clipboard, in this way, the cmd can be minimized. assign the answer to q to prevent q! = GetText () is used to match the answer...

There may be three questions in the test questions: single-choice questions, multiple-choice questions, and question-making questions, which are divided into three sheets. if the questions are not found in a sheet, they are matched with another sheet. Complete Code is attached:

#coding=utf-8try:    import xlrd    import win32clipboard as w    import win32con    import re    import time    import os    import _winregexcept Exception,e:    print edef getText():     w.OpenClipboard()     d = w.GetClipboardData(win32con.CF_TEXT)     w.CloseClipboard()     return d  def setText(aString):    w.OpenClipboard()    w.EmptyClipboard()    w.SetClipboardData(win32con.CF_TEXT, aString)    w.CloseClipboard()try:    def get_desktop():        try:            key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,                                  r'Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders')            return _winreg.QueryValueEx(key, "Desktop")[0]        except Exception,e:            print eexcept Exception,e1:    print e1try:    xlsfile = xlrd.open_workbook(get_desktop()+'\\exercise.xls')except Exception,e:    print e    try:    mysheet = xlsfile.sheet_by_name('1')except Exception,e:    print e'''try:    print('%d rows, %d cols'%(mysheet.nrows, mysheet.ncols))except Exception,e:    print e'''q = ''try:    while(1):        if(q != getText()):            q=getText()        else:            time.sleep(1)            continue        flag = 0        for row in range(mysheet.nrows):            if(re.search('.*'+q.decode('gb2312')+'.*', mysheet.cell(row,0).value)!=None):                os.system('cls')                flag = 1                try:                    print mysheet.cell(row,0).value                except Exception,e:                    print e                print 'Answer: '+mysheet.cell(row,7).value                setText(mysheet.cell(row,7).value)                q = mysheet.cell(row,7).value                break        if(flag == 0):            tsheet = xlsfile.sheet_by_name('2')            for row in range(tsheet.nrows):                if(re.search('.*'+q.decode('gb2312')+'.*', tsheet.cell(row,0).value)!=None):                    os.system('cls')                    flag = 1                    try:                        print tsheet.cell(row,0).value                    except Exception,e:                        print e                    print 'Answer: '+tsheet.cell(row,2).value                    setText(tsheet.cell(row,2).value.encode('gb2312'))                    q = tsheet.cell(row,2).value.encode('gb2312')                    break        if(flag == 0):            tsheet = xlsfile.sheet_by_name('3')            for row in range(tsheet.nrows):                if(re.search('.*'+q.decode('gb2312')+'.*', tsheet.cell(row,0).value)!=None):                    os.system('cls')                    flag = 1                    try:                        print tsheet.cell(row,0).value                    except Exception,e:                        print e                    print 'Answer: '+tsheet.cell(row,7).value                    setText(tsheet.cell(row,7).value.encode('gb2312'))                    q = tsheet.cell(row,7).value.encode('gb2312')except Exception, e:    print EmptyClipboard

Notes:

1. excel must be used on the computer, not WPS, because the connected database operates on excel.

Excel must be used on the computer, not WPS, because the connected database is used for excel operations.

Excel must be used on the computer, not WPS, because the connected database is used for excel operations.

It's important to say it three times.

2. in this case, there is no problem. Don't worry, Answer is still credible. this is a python encoding problem = and you don't know how to solve it ..

3. I tested 600 questions and solved a lot of bugs. it should not be used in the case of half errors. Attach the test results. (if you eat again during the test, you will not be able to shake your hands ..)

Related Article

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.