日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python用os模块自动打开软件_Python实现自动打开电脑应用的示例代码

發布時間:2025/4/16 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python用os模块自动打开软件_Python实现自动打开电脑应用的示例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

通過找出找出軟件在電腦中快捷方式的位置(電腦屏幕中的坐標),使用代碼模擬鼠標進行雙擊打開。

通過輸入軟件在電腦中的安裝路徑打開軟件。

1.第一種方法:

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

import pyautogui

import time

#循環執行pyautogui.position()獲取不同位置坐標

while True:

print("當前鼠標的坐標為:"pyautogui.position())

time.sleep(1)#設置打印的時間間隔

多次執行結果:

在使用此方法獲取到想要打開的軟件的快捷方式后,就是進行鼠標點擊的模擬了

我們可以通過使用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,216)

position=[startPosition,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 自動打開電腦應用內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持我們!

本文標題: Python實現自動打開電腦應用的示例代碼

本文地址: http://www.cppcns.com/jiaoben/python/308336.html

總結

以上是生活随笔為你收集整理的python用os模块自动打开软件_Python实现自动打开电脑应用的示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。