我想写一个程序,可以:
打开一个应用程序。 (我使用subprocess
模块成功执行了应用程序)
使用该应用程序。 (如何在该特定软件上自动执行任务?是否可能?例如,该软件需要我登录和python并自动使用我的凭据登录并继续自动化我的典型用法,如打开项目)
关闭应用程序。
from datetime import datetime
import time
import os
import subprocess
os.chdir("../../Desktop/Sublime/Sublime Text 2") #Directory need to adjust to find teamviewer
print "Starting python..."
switch = True
activator1 = True
activator2 = True
startBoolean = True
exitBoolean = True
container = None
def startTimer(startTime , exitTime):
global dateTime
global startBoolean
global exitBoolean
dateTime = datetime.now().strftime('%H%M%S')
print "Current Time: " +dateTime+ "| Start Time: " +startTime +"| Exit Time: "+exitTime
if dateTime >= startTime:
startBoolean = False
print "start boolean: "+ str(startBoolean)
if dateTime >= exitTime:
exitBoolean = False
print "exit boolean: "+ str(exitBoolean)
return [startBoolean,exitBoolean]
def startApplication(startTime):
if dateTime >= startTime:
print "Launching Application..."
process = subprocess.Popen(["./sublime_text"],shell=True) #This is using ubuntu terminal command. Might need to change the command to fits other OS.
def stopApplication(exitTime):
if dateTime >= exitTime:
print "Exiting Application..."
process = subprocess.Popen(["killall sublime_text"],shell=True)
startTime = raw_input("Project Launch Time: ")
exitTime = raw_input("Project Exit time: ")
while switch:
startTimer(startTime , exitTime)
global container
container = startTimer(startTime , exitTime)
if container!=None:
if activator1==True:
if container[0] == False:
print "clear1"
activator1 = False #Cannot activate < 1.
startApplication(startTime)
if activator2==True:
if container[1] == False:
print "clear2"
activator2 = False #Cannot activate < 1.
stopApplication(exitTime)
答案 0 :(得分:1)
您好,您可以使用pyAutoIt
我正在为记事本提供示例代码,以便将其保存在所需的位置和所需的路径中。
import autoit,time
autoit.run("notepad.exe")
autoit.win_wait_active("Untitled - Notepad")
time.sleep(5)
autoit.control_send("Untitled - Notepad", "Edit1", "hello world{!}")
autoit.control_send("Untitled - Notepad", "Edit1", "test-2")
#time.sleep(5)
autoit.win_close("Untitled - Notepad")
autoit.control_click("Notepad", "Button1")
#time.sleep(5)
autoit.win_wait_active("Save As")
#time.sleep(5)
autoit.control_send("Save As", "Edit1","Path-to-save")
autoit.control_send("Save As", "Edit1", "file-name")
autoit.control_click("Save As", "Button1")
答案 1 :(得分:0)
pywinauto也是Python上非常棒的GUI自动化解决方案。
示例:
from pywinauto import Application
app = Application().start(r"C:\Program Files\Sublime Text 2\sublime_text.exe")
app.untitled_SublimeText2.Wait('ready', timeout=10) # just for possible slow start
app.untitled_SublimeText2.MenuSelect('File->Open file')
app.Open.FilenameEdit.SetText('path to the project')
app.Open.Open.Click()
app.Open.WaitNot('visible') # just to make sure it's closed
main_window = app["project_name - Sublime Text 2"]
# print access names for all available controls on the main window
main_window.PrintControlIdentifiers()
还有GUI助手SWAPY,pywinauto对象检查器和代码生成器。