Development of small program for finding answers in test questions library

Source: Internet
Author: User

After the final exam bored in the test paper library. Then Cy came to my bedroom, remind me to get an automatic in Excel to find the answer to the program, he gave the idea immediately after the start of work.

The so-called question Bank with our school features is to download Excel forms, homework and test questions are on the page, we want to find the answer in Excel. This process, the program mainly to achieve the following functions:

1, monitor the change of clipboard;

2, if the Clipboard content changes detected, according to the contents of the Clipboard, the use of regular expressions in the question bank questions to find the first most matching topic;

3, output the answer in the console.

Considering that Python has a rich library, it's not a hassle to connect to Excel or monitor the Clipboard. Python was then developed.

XLRD is used to implement reading of Excel files, Pythonwin is used to monitor the Clipboard.

def GetText ():     w.openclipboard ()     = w.getclipboarddata (win32con. Cf_text)     W.closeclipboard ()     return  D  def  SetText (astring):    W.openclipboard ()    w.emptyclipboard ()    w.setclipboarddata (Win32con. Cf_text, astring)    W.closeclipboard ()

Search the function on the Web, realize the contents of the Clipboard and set the contents of the Clipboard.

Read the Excel file on the desktop. Get the desktop location from the registry

def get_desktop ():
= _winreg. Openkey (_winreg. HKEY_CURRENT_USER, R'Software\\microsoft\\windows\\currentversion\\explorer\\shell Folders') return"Desktop") [0]

After that, open the. xls file by using the XLRD library function and select sheet

    Xlsfile = Xlrd.open_workbook (get_desktop () +'\\exercise.xls')    = Xlsfile.sheet_by_name ('1')

You then need to implement the ability to monitor the Clipboard. Listener this kind of thing is too troublesome I do not do, so every 1s check the contents of the Clipboard changes.

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

After getting the contents of the Clipboard, match the topic with regular expressions and output the answer if the match succeeds. This involves coding issues, I do not know why this writing, anyway, is realized ...

 forRowinchRange (mysheet.nrows):if(Re.search ('.*'+q.decode ('gb2312')+'.*', Mysheet.cell (row,0). value)! =None): Os.system ('CLS') Flag= 1Try:                    PrintMysheet.cell (row,0). Valueexceptexception,e:PrintePrint 'Answer:'+mysheet.cell (row,7). Value SetText (Mysheet.cell (Row,7). Value) Q= Mysheet.cell (row,7). ValueBreak 

Why SetText here (Mysheet.cell (row,7). Value) now, because the mouse is not allowed to leave the browser when the test, the CMD is blatantly put on the wrong again, so the search answers put in the clipboard, So the cmd is minimized, the answer is assigned to Q to prevent q! = GetText () to match the answer, hehe hey ...

There are three types of question questions: single-choice, multi-choice and judgment, are divided into three sheet, if the topic is not found in some sheet to change the other sheet matching problems. Enclose the complete code:

#Coding=utf-8Try:    ImportxlrdImportWin32clipboard as WImportWin32conImportReImport TimeImportOSImport_winregexceptexception,e:PrintedefGetText (): W.openclipboard () d=W.getclipboarddata (Win32con. Cf_text) W.closeclipboard ()returnDdefSetText (astring): W.openclipboard () W.emptyclipboard () W.setclipboarddata (Win32con. Cf_text, astring) W.closeclipboard ()Try:    defget_desktop ():Try: Key=_winreg. Openkey (_winreg. HKEY_CURRENT_USER, R'Software\\microsoft\\windows\\currentversion\\explorer\\shell Folders')            return_winreg. QueryValueEx (Key,"Desktop") [0]exceptexception,e:Printeexceptexception,e1:PrintE1Try: Xlsfile= Xlrd.open_workbook (Get_desktop () +'\\exercise.xls')exceptexception,e:PrinteTry: MySheet= Xlsfile.sheet_by_name ('1')exceptexception,e:Printe" "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)            ContinueFlag=0 forRowinchRange (mysheet.nrows):if(Re.search ('.*'+q.decode ('gb2312')+'.*', Mysheet.cell (row,0). value)! =None): Os.system ('CLS') Flag= 1Try:                    PrintMysheet.cell (row,0). Valueexceptexception,e:PrintePrint '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')             forRowinchRange (tsheet.nrows):if(Re.search ('.*'+q.decode ('gb2312')+'.*', Tsheet.cell (row,0). value)! =None): Os.system ('CLS') Flag= 1Try:                        PrintTsheet.cell (row,0). Valueexceptexception,e:PrintePrint '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')             forRowinchRange (tsheet.nrows):if(Re.search ('.*'+q.decode ('gb2312')+'.*', Tsheet.cell (row,0). value)! =None): Os.system ('CLS') Flag= 1Try:                        PrintTsheet.cell (row,0). Valueexceptexception,e:PrintePrint 'Answer:'+tsheet.cell (row,7). Value SetText (Tsheet.cell (Row,7). Value.encode ('gb2312')) Q= Tsheet.cell (row,7). Value.encode ('gb2312')exceptException, E:PrintEmptyClipboard

A few notes:

1, the use of the computer must have Excel, cannot be WPS, because the connected library is the Excel operation

You must have Excel on the computer you are using, and you cannot be WPS because the connected library is an Excel operation.

You must have Excel on the computer you are using, and you cannot be WPS because the connected library is an Excel operation.

Because it's important, so say it three times.

2, encountered this situation, did not appear the topic, do not panic, answer or credible, this is the problem of Python coding = = Do not know how to solve.

3, tested 600 questions, solved a lot of bugs, should not appear to use half of the error situation. Attach the test results (when you eat again during the test, you can't help shaking your hands .) )

Development of small program for finding answers in test questions library

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.