1. 程式人生 > 程式設計 >Python實現自動開啟電腦應用的示例程式碼

Python實現自動開啟電腦應用的示例程式碼

由於時間原因,有時候可能會錯過某個上網課的時間段。因此想要實現自動定時啟動DingDing。

新手一枚,如有不當勿噴望大佬指正。

自動開啟DingDing可以由兩種方法實現:

  • 通過找出找出軟體在電腦中快捷方式的位置(電腦螢幕中的座標),使用程式碼模擬滑鼠進行雙擊開啟。
  • 通過輸入軟體在電腦中的安裝路徑開啟軟體。

1.第一種方法:

​在python中,使用pip install pyautogui 安裝第三方庫,在此庫中,可以使用pyautogui.position()獲取當前滑鼠放置位置的座標。我們可以多次使用此方法來實現獲取任意想要獲取位置的座標。

import pyautogui
import time
#迴圈執行pyautogui.position()獲取不同位置座標
while True:
 print("當前滑鼠的座標為:"pyautogui.position())
 time.sleep(1)#設定列印的時間間隔

多次執行結果:

Python實現自動開啟電腦應用的示例程式碼

在使用此方法獲取到想要開啟的軟體的快捷方式後,就是進行滑鼠點選的模擬了

我們可以通過使用pyautogui.click(click=2)實現雙擊滑鼠左鍵的效果。

通使用pyautogui.moveTo(x,y)實現滑鼠的移動功能。結合滑鼠的點選就可以進行自動的開啟電腦應用的功能了。

import pyautogui
import time
def AutoOpen():
 startPosition = (327,164)#滑鼠需要移動的位置
 endPosition = (306,216)
 position=[startPosition,endPosition]
 for i in position:
  pyautogui.moveTo(i)#控制滑鼠移動
  pyautogui.click(clicks=2)#實現滑鼠雙擊
  time.sleep(3)
if __name__ == '__main__':
 AutoOpen()

需要注意的是:本方法不能再程式碼的編譯器佔滿整個螢幕的時候使用,那樣獲取的座標位置為編譯器中的位置,位置雖然通用,但是不能實現點選應用的功能,要點選的應用不能被編譯器所覆蓋。只有這樣才能實現點選功能。

2.第二種方法

獲取檔案的安裝路徑,找到字尾為.exe的可執行的檔案,使用os.startfile(Path)開啟檔案(os庫為自帶庫無需安裝)Path=“F:\XXX\XXX.exe”

import os 
Path = r'F:\DingDing\DingtalkLauncher.exe'
os.startfile(Path)

通過上面三行程式碼足以開啟需要開啟的檔案。

import pyautogui
import time
import os
def AutoOpen(Path):
 os.startfile(Path) #os.startfile()開啟外部應該程式,與windows雙擊相同
 pyautogui.moveTo(306,216)#pyautogui.moveTo()將滑鼠移動到指定位置
 time.sleep(6)
 pyautogui.click(clicks=2)#滑鼠點選,實現滑鼠雙擊
if __name__ == '__main__':
 Path=r'F:\DingDing\DingtalkLauncher.exe'
 AutoOpen()

此方法如果不涉及點選事件的模擬則沒有要求,如果需要點選則同上,不能覆蓋住要點選的位置。

3.定時開啟

在自動開啟的功能實現後,就是簡單的設定自動開啟的時間了,通過使用time 庫,獲取當前時間。自己可以設定一個需要開啟的時間,通過對比當前時間就能實現定時自動開啟的功能了。

完整程式碼:

import pyautogui
import time

def open_app(Path):
 os.startfile(Path) #os.startfile()開啟外部應該程式,與windows雙擊相同
 pyautogui.moveTo(306,216)#pyautogui.moveTo()將滑鼠移動到指定位置
 time.sleep(6)
 pyautogui.click(clicks=2)#滑鼠點選,實現滑鼠雙擊
def AutoOpen():
 startPosition = (327,164)
 endPosition = (306,endPosition]
 for i in position:
  pyautogui.moveTo(i)
  pyautogui.click(clicks=2)
  time.sleep(3)
if __name__ == '__main__':
 Path=r'F:\DingDing\DingtalkLauncher.exe'
 times = "2020-xx-xx xx:xx"#設定需要開啟的時間,此時間看自己需求是否精確到秒("2020-xx-xx xx:xx:xx")
 while True:
  nowtime = time.strftime('%Y-%m-%d %H:%M')
  if (times == nowtime):
   open_app(Path)
   break
  else:
   print(time.strftime('%Y-%m-%d %H:%M:%S'))
   time.sleep(10)

python自動化開啟網頁

from selenium.webdriver.firefox.options import Options as FOptions
from selenium.webdriver.chrome.options import Options as Foptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

#firefox設定代理
profile = FirefoxProfile()
# 啟用手動代理配置(對應著在 profile(配置檔案)中設定首選項)
profile.set_preference("network.proxy.type",1)
# ip及其埠號配置為 http 協議代理
profile.set_preference("network.proxy.http","127.0.0.1")
profile.set_preference("network.proxy.http_port",8080)

# 所有協議共用一種 ip 及埠,如果單獨配置,不必設定該項,因為其預設為 False
profile.set_preference("network.proxy.share_proxy_settings",True)

#chrome設定代理
# options = FOptions()


options = FOptions()
chrome_options = webdriver.FirefoxOptions()
chrome_options.add_argument('--proxy-server=http://127.0.0.1:8080')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('disable-infobars')
browser = webdriver.Firefox(executable_path="D:/geckodriver.exe",firefox_profile=profile)

browser.maximize_window()
browser.get('https://account.dianping.com/login?redir=http%3A%2F%2Fwww.dianping.com%2F')

button = browser.find_element_by_xpath('/html/body/div/div[2]/div[5]/span')
button.click()

到此這篇關於Python實現自動開啟電腦應用的示例程式碼的文章就介紹到這了,更多相關Python 自動開啟電腦應用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!