亲宝软件园·资讯

展开

python自动化工具之pywinauto实例详解

人气:9

一、win环境应用自动化

1.浏览器中下载

2.在cmd下启动:python get-pip.py

3.在cmd中输入python -m pip --version查看安装pip的版本。

4.模块安装:pip3 install pywinauto

5.程序中加载模块:

import time
from pywinautoimport application

二、实例

#encoding=utf-8
#author: walker
#date: 2018-04-26
#summary: 以 pywinauto 调用 ABBYY FineReader 12 Professional 将 pdf 另存为 txt
#sys.version:'3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]'
 
import os, sys, time, shutil, ctypes
from pywinauto.application import Application
 
# 转换单个 pdf 为 txt
# 需要保证输出 txt 文件所在目录是存在的,txt 文件不存在
# 外层许捕获异常
def Pdf2Txt(pdfFile, txtFile):
 print('Enter Pdf2Txt ...')
 
 FineReaderPath = r'F:\software\ABBYY FineReader 12\FineReader.exe'
 UsualTimeout = 30 
 
 cmd = r'"%s" "%s"' % (FineReaderPath, pdfFile)
 print('cmd: %s' % cmd)
 app = Application(backend="win32").start(cmd)
 
 mainWindow = app.window(class_name=r'FineReader12MainWindowClass')
 mainWindow.wait("ready", timeout=UsualTimeout)
 
 statButton = mainWindow.child_window(process=app.process, class_name='Button', title_re=r'\d+ OF \d+ PAGES RECOGNIZED')
 
 statButton.wait("exists visible", timeout=UsualTimeout)
 statButton.wait_not("visible", timeout=RecognizeTimeout) # 具体转换可能要很久
 
 mainWindow.dump_tree(filename='mainWindow2.txt')
 
 mainWindow.ToolbarSave.wait('enabled', timeout=RecognizeTimeout).click() # 点击保存按钮
 
 saveDlg = app.window(class_name=r'#32770', title=r'Save document as')
 saveDlg.wait("exists visible", timeout=UsualTimeout)
 saveDlg.dump_tree(filename='saveDlg.txt')
 saveDlg.DUIViewWndClassName.DirectUIHWND.ComboBox2.select('Text (*.txt)') # 设置保存格式
 saveDlg.DUIViewWndClassName.DirectUIHWND.FloatNotifySink.ComboBox.Edit.set_text(txtFile) # 设置保存文件名
 
 saveDlg.DUIViewWndClassName.DirectUIHWND.CheckBox.UncheckByClick() # 取消复选框的选中状态(保存后打开)
 
 # saveDlg.type_keys(r'%S') # 保存为 txt(Alt + S)
 saveDlg.child_window(title="保存(&S)", class_name="Button").wait('ready', timeout=RecognizeTimeout).click() # 点击保存按钮
 print('保存(&S) Button clicked!')
 
 saveDlg.wait_not("exists", timeout=UsualTimeout)
 
 cnt = 0
 for _ in range(0, 100):
 time.sleep(0.1)
 cnt += 1
 if os.path.exists(txtFile):
 break
 
 app.kill()
 
 saveDlg.wait_not("exists", timeout=UsualTimeout)
 mainWindow.wait_not("exists", timeout=20)
 print('not exists mainWindow!')
 print('current time:%f (%s)' % (time.time(), time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())))
 
 return True
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论