pyautogui应用:CiTdxWGuanbi.py(自动 盘后数据下载 及 数据维护 并 关机)

# -*- coding: utf-8 -*-

'''
深入:
pywin32模块:  窗口、鼠标键盘、文件、进程等控制


'''





#封装函数  自动盘后数据下载和数据维护  结合pygetwindow模块实现

import pyautogui
import pygetwindow as gw
import time

pyautogui.FAILSAFE=False    #添加失控保险  鼠标指向左上角 即point(x,y)=0,0则停止程序
pyautogui.PAUSE=1           #添加暂停时间 作用于每一个pyautogui方法  即方法执行后暂停0.5秒



# =============================================================================
# #数据下载
# =============================================================================
def shujuxiazai():
    
#盘后数据下载
    pyautogui.click('TdxW定位图片/200.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.typewrite(['down']*11)
    pyautogui.press('enter')
    
    pyautogui.click('TdxW定位图片/202沪深日线.png',duration=1,tween=pyautogui.easeInOutQuad)
    
    pyautogui.click('TdxW定位图片/202日线和实时行情数据.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.typewrite(['right','right','enter'])
    #pyautogui.typewrite(['right','right','num0','num1','enter'])
    
    while not(pyautogui.locateOnScreen('TdxW定位图片/202下载完成.png',grayscale=True)):
        time.sleep(0.01)
        if pyautogui.locateOnScreen('TdxW定位图片/202下载被取消.png',grayscale=True):
            break
    pyautogui.click('TdxW定位图片/202关闭.png',duration=1,tween=pyautogui.easeInOutQuad)
    
    
    
    
# =============================================================================
# #数据下载_财务数据
# =============================================================================
def shujuxiazai_caiwushuju():
    
#专业财务数据
    pyautogui.click('TdxW定位图片/200.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.typewrite(['down']*12)
    pyautogui.press('enter')
    
    time.sleep(1)
    if len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203没有需要下载的数据包.png',grayscale=True)))==2:
        pyautogui.click('TdxW定位图片/203关闭.png',duration=1,tween=pyautogui.easeInOutQuad)

    else:     
#点击开始下载
        jsq203=0
        sTime203=time.time()
        while not pyautogui.locateOnScreen('TdxW定位图片/203开始下载.png',grayscale=True):
            time.sleep(0.01)

            jsq203=time.time()-sTime203
            if jsq203>=60:
                print('error:寻找203开始下载超时...')
    
            
        pyautogui.click('TdxW定位图片/203开始下载.png',duration=1,tween=pyautogui.easeInOutQuad)
        pyautogui.moveRel(0,-100,duration=0.5,tween=pyautogui.easeInOutQuad)
    
#如果需要,继续点击开始下载
        while (len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203下载完成.png'))) != 2):
            time.sleep(0.01)
            while pyautogui.locateOnScreen('TdxW定位图片/203开始下载.png',grayscale=True):
                try:
                    pyautogui.click('TdxW定位图片/203开始下载.png',duration=1,tween=pyautogui.easeInOutQuad)
                    pyautogui.moveRel(0,-100,duration=0.5,tween=pyautogui.easeInOutQuad)
                except:
                    pass
                if (len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203下载完成.png'))) == 2):
                    break
                    
        w203=0
        while w203==0:
            pyautogui.click('TdxW定位图片/203开始下载.png',duration=1,tween=pyautogui.easeInOutQuad)
            pyautogui.moveRel(0,100,duration=0.5,tween=pyautogui.easeInOutQuad)
                
            if (len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203下载完成.png'))) == 2):
                w203=1
    
    
#点击关闭
        pyautogui.click('TdxW定位图片/203关闭.png',duration=1,tween=pyautogui.easeInOutQuad)
    
    
        #点击关闭以后可能出现的意外情况处理:
        time.sleep(1)
        while pyautogui.locateOnScreen('TdxW定位图片/203关闭后取消下载吗.png'):
            pyautogui.click('TdxW定位图片/203关闭后取消.png',duration=1,tween=pyautogui.easeInOutQuad)
    
            #点击开始下载
    
            while not pyautogui.locateOnScreen('TdxW定位图片/203开始下载_关闭后.png',grayscale=True):
                time.sleep(0.01)
                print('error:没有找找到203开始下载...')
    
            pyautogui.click('TdxW定位图片/203开始下载_关闭后.png',duration=1,tween=pyautogui.easeInOutQuad)
            pyautogui.moveRel(0,-100,duration=0.5,tween=pyautogui.easeInOutQuad)
        
            #如果需要,继续点击开始下载
            while (len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203下载完成.png'))) != 2):
                time.sleep(0.01)
                while pyautogui.locateOnScreen('TdxW定位图片/203开始下载.png',grayscale=True):
                    try:
                        pyautogui.click('TdxW定位图片/203开始下载.png',duration=1,tween=pyautogui.easeInOutQuad)
                        pyautogui.moveRel(0,-100,duration=0.5,tween=pyautogui.easeInOutQuad)
                    except:
                        pass
                    if (len(list(pyautogui.locateAllOnScreen('TdxW定位图片/203下载完成.png'))) == 2):
                        pyautogui.click('TdxW定位图片/203开始下载.png',duration=1,tween=pyautogui.easeInOutQuad)
            #点击关闭
            pyautogui.click('TdxW定位图片/203关闭.png',duration=1,tween=pyautogui.easeInOutQuad)
    
            



    
# =============================================================================
# #数据维护
# =============================================================================
def shujuweihu():
    pyautogui.click('TdxW定位图片/200.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.typewrite(['down']*14)
    pyautogui.press('enter')
    
    while not pyautogui.locateOnScreen('TdxW定位图片/204数据恢复.png',grayscale=True):
        time.sleep(0.01)
    pyautogui.click('TdxW定位图片/204数据恢复.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.click('TdxW定位图片/204浏览.png',duration=1,tween=pyautogui.easeInOutQuad)

    pyautogui.press(['down']*2)
    pyautogui.press(['right'])
    pyautogui.press(['down','right'])
    pyautogui.press(['down']*9)
    pyautogui.press(['enter'])
    
    pyautogui.press(['tab']*8)
    pyautogui.press(['space','space'])

    while not pyautogui.locateOnScreen('TdxW定位图片/204T0002.png',grayscale=True):
        time.sleep(0.01)
        print('选择提醒:未成功选择目录T0002文件夹。')
    pyautogui.click('TdxW定位图片/204执行操作.png',duration=1,tween=pyautogui.easeInOutQuad)
    
    while not pyautogui.locateOnScreen('TdxW定位图片/204确认操作.png',grayscale=True):
        time.sleep(0.01)
    pyautogui.click('TdxW定位图片/204确认操作.png',duration=1,tween=pyautogui.easeInOutQuad)
   
    while not pyautogui.locateOnScreen('TdxW定位图片/204操作成功.png',grayscale=True):
        time.sleep(0.01)
    pyautogui.click('TdxW定位图片/204操作成功.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.press('enter')

    while not pyautogui.locateOnScreen('TdxW定位图片/204恢复完毕.png',grayscale=True):
        time.sleep(0.01)
    pyautogui.click('TdxW定位图片/204恢复完毕.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.hotkey('alt','f4')
    




# =============================================================================
# #退出系统1
# =============================================================================
def xitongtuichu():
    
    pyautogui.click('TdxW定位图片/200.png',duration=1,tween=pyautogui.easeInOutQuad)
    pyautogui.typewrite(['down']*16)
    pyautogui.press(['enter']*2)
    
    
    #点击退出以后可能出现的意外情况处理:
    if pyautogui.locateOnScreen('TdxW定位图片/203系统退出后取消.png'):
        pyautogui.click('TdxW定位图片/203系统退出后取消.png',duration=1,tween=pyautogui.easeInOutQuad)
        






if __name__ == '__main__':
# =============================================================================
# #循环调用所有应用程序 至主屏 准备数据下载 然后关闭 
# =============================================================================
    screenWidth,screenHight=pyautogui.size()
    #八屏Box数据
    REGION=(-screenWidth,-screenHight*2,screenWidth*3,screenHight*3)
    #pyautogui.screenshot('截屏定位图片/eightScreen.png',region=REGION)
    
    tdxWX,tdxWY=0,screenHight-350
    for tdxW in gw.getWindowsWithTitle('通达信金融终端V7.46'):
#        print(tdxW)
    
        tdxW.maximize()
        tdxW.restore()
        tdxW.moveTo(tdxWX,tdxWY)
        tdxWX+=160
        tdxWY+=-100
        
        shujuxiazai()
        shujuxiazai_caiwushuju()
        shujuweihu()
#        xitongtuichu()
        

    
#    for tdxWXT in pyautogui.locateAllOnScreen('TdxW定位图片/200.png',grayscale=True):
#        #pirnt(tdxWXT)
#        
#        shujuxiazai()
#        xitongtuichu()
        
        
        
    # =============================================================================
    # #自建函数封装提醒方式:手机短信提醒  及  邮件提醒
    # =============================================================================
    import os,sys,time
    
    sys.path.append('F:\\SpyderXIANGMUWENJIANJIA\\编程相关\\smtplib邮件相关')
    sys.path.append('F:\\SpyderXIANGMUWENJIANJIA\\编程相关\\twilio手机短信电话相关')
    from stmplibSendMsg import sendMsg
    from twilioSendSMS import sendSMS
    
    
    sendTime=time.strftime('%Y/%m/%d',time.localtime(time.time()))
    djs=180
    
    #发送手机短信提醒
    sendSMS('{}盘后工作完成,{}秒后电脑自动关闭...'.format(sendTime,djs))
    
    #发送邮件提醒
    sendMsg(toAddr='[email protected]',
            msgSub='{}盘后工作完成,{}秒后电脑自动关闭...'.format(sendTime,djs),
            msgText='')
    
    #CMD关机命令  (CMD取消命令:shutdown -a)
    os.system('shutdown -s -t {}'.format(djs)) 
    
    ###############################################################################

你可能感兴趣的:(编程相关)