背景
最近遇到一个需求,需要去导出大量数据,但是数据导出程序并不是我写的。最主要是厂家给的程序最大只能支持七天(必选吐糟),其中光等待写入本地时间都要十分钟左右。我粗略的算了一下,一天平均1千多条数据,让我导三年的数据,如果用手动操作的话工期估计要10天左右,所以决定用python中的工具类pywinauto 来实现。
(1)思路
我需要程序自动向pc端exe文件系统填入开始时间、结束时间、然后自动点击开始导出按钮开始执行导出,然后10分钟后开始时间=结束时间+1,结束时间=结束时间+6,再设置最大时间,当最大时间小于结束时间时,本次任务结束。
(2)环境搭建
(1)python3.6
(3)代码实现
"""
窗口控件的分类:
状态栏:StatusBar 静态内容:Static
按钮:Button 复选框:CheckBOX
单选框:RadioButton 组框:GroupBOX
组合框:ComboxBox 对话框(窗口):Dialog
编辑栏:Edit 头部内容:Header
列表框:ListBox 列表显示控件:ListView
弹出菜单:PopupMenu 选项卡控件:TabControl
工具栏:Toolbar 工具提示:ToolTips
树状视图:Tree View Menu:菜单
Menultem:菜单项 Pane:窗格
"""
from pywinauto import Application
import time
import datetime
import configparser
# os.system("pause")
#sys.setdefaultencoding('utf-8')
# 启动 navicat
# app = Application("uia").start(r"F:\exportData\RealBaseToPostgre.exe")
cf = configparser.ConfigParser()
cf.read("config.ini",encoding='utf-8-sig')
secs = cf.sections() # 获取文件中所有的section(一个配置文件中可以有多个配置,如数据库相关的配置,邮箱相关的配置,secs = cf.sections() # 获取文件中所有的section(一个配置文件中可以有多个配置,如数据库相关的配置,邮箱相关的配置,
#每个section由[]包裹,即[section]),并以列表的形式返回
path=cf.get("info","path")
sleepTime = cf.get("info", "sleep")
initime = cf.get("info", "initime")
app = Application("uia").start(r""+path)
print('等待:',initime,'秒程序初始化完成')
startDate=cf.get("info","startDate")
endDate=cf.get("info","endDate")
id=cf.get("info","id")
maxDate = cf.get('info','maxDate')
maxDate = (datetime.datetime.strptime(str(maxDate), "%Y-%m-%d") + datetime.timedelta(days=0)).strftime("%Y-%m-%d")
print(maxDate)
count = 0
#print('1:',startDate,endDate)
while (True):
if (endDate >= maxDate):
endDate = maxDate
print('结束日期已超过最大日期,现将结束日期的值设置为最大日期!')
print('The count is:', count)
count = count + 1
# 通过窗口标题去选择窗口
dlg = app["TN8000新旧数据转换程序"]
edit = dlg['Edit']
edit2 = dlg['Edit1']
edit1 = dlg['Edit2']
edit3 = dlg['Edit3']
# edit.type_keys('edit')
# send_keys(Keys.SPACE)
edit1.type_keys(startDate+"{SPACE}00:00:00")
edit2.type_keys(endDate+"{SPACE}23:59:59")
edit3.type_keys(id)
button1 = dlg["开始导出"]
button1.click()
print('已经点击了导出按钮,现在等待:', sleepTime, '秒!')
time.sleep(int(sleepTime))#休眠x秒钟
##开始时间+1
mStartDate = (datetime.datetime.strptime(str(endDate), "%Y-%m-%d") + datetime.timedelta(days=1)).strftime(
"%Y-%m-%d")
##结束时间7天
mEndate = (datetime.datetime.strptime(str(endDate), "%Y-%m-%d") + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
startDate=mStartDate
# endDate=startDate
endDate=mEndate
#print('2:', startDate, endDate)
if(startDate>maxDate):
print('开始日期已经超过最大日期,准备结束......')
break
# datetime.datetime.strptime(str(startDate), "%Y-%m-%d") + datetime.timedelta(days=1).strftime("%Y-%m-%d")
# endDate=datetime.datetime.strptime(str(startDate), "%Y-%m-%d")+datetime.timedelta(days=1).strftime("%Y-%m-%d")
# print (endDate)
print("Good bye!")
# # button_close = dlg['关闭']
# dlg = app["TN8000新旧数据转换程序"]
# # 打印窗口中所有的控件
# dlg.print_control_identifiers()
# # print(dlg.print_control_identifiers())
思路讲解
1.通过Application启动xx.exe文件,然后通过exe文件通过窗口标题去选择窗口的界面控件,可以首先获取dlg.print_control_identifiers()所有控件,找到对应的控件名称后就可以对控件操作了。
(4)config.ini配置
[info]
path=F:\exportData\xx.exe
startDate=2018-01-01
endDate=2018-01-06
#maxDate=2021-05-01
maxDate=2021-05-01
id=1
max=4
sleep=120
#程序初始化时间秒
initime = 10
configPath=config.ini
(5)效果
(5)编译打包为.exe文件
说明:在这里遇到了一点小坑,最后还是选择cx-freeze来进行打包为exe文件,具体操作如下:
(1)安装cx-freeze插件
(2)打包
cmd到项目目录下.\venv\Scripts..,先拷贝路径,比如我的路径:F:\PythonWorkSpace\untitled3\venv\Scripts\cxfreeze.exe
在Terminal打开输入:
F:\PythonWorkSpace\untitled3\venv\Scripts\cxfreeze.exe winauto.py --target-dir dist
其中target-dir dist为表示为将打包后的文件存放于当前目录dist下,效果如下:
end:最后我们双击exe文件即可运行。