pywinauto实战-操作MS Paint

"""
Example script for MS Paint
自动打开png图片,调整像素后保存
1、先安装PYTHON和pywinauto库(参考网上教程)
Requirements:
  - tested on Windows 10 (should work on Win7+)
  - pywinauto 0.6.1+
"""
from __future__ import print_function
import logging
from pywinauto import actionlogger
from pywinauto import Application
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--log", help = "enable logging", type=str, required = False)
args = parser.parse_args()
actionlogger.enable()
logger = logging.getLogger('pywinauto')
if args.log:
    logger.handlers[0] = logging.FileHandler(args.log)
app = Application(backend='uia').start(r'mspaint.exe')
dlg = app.window(title_re='.* - 画图')
# File->Open menu selection
dlg["“文件”选项卡"].click()
dlg.child_window(title='打开', control_type='MenuItem', found_index=0).invoke()
#dlg.menu_select(r"文件->打开")
# handle Open dialog
file_name_edit = dlg['打开'].child_window(title="文件名(N):", control_type="Edit")
file_name_edit.set_text(r'D:\1.png')
# There are 2 Open buttons:
# dlg.Open.Open.click() will call drop down list of the file name combo box.
# The child_window statement is just copied from print_control_identifiers().
dlg['打开'].child_window(title="打开(O)", auto_id="1", control_type="Button").click()
dlg.重新调整大小.click()
dlg.调整大小和扭曲.像素.select()
if dlg.调整大小和扭曲.保持纵横比.get_toggle_state() != 1:
    dlg.调整大小和扭曲.保持纵横比.toggle()
dlg.调整大小和扭曲.child_window(title="水平(H):", control_type="Edit").set_text('90')
dlg.调整大小和扭曲.确定.click()
# Select menu "File->Save as->PNG picture"
dlg["“文件”选项卡"].click()
dlg.child_window(title="另存为", found_index=1).invoke()
dlg['保存为'].child_window(title='文件名:',control_type="Edit").set_text('test.png')
dlg['保存为'].保存.click()
if dlg.确认另存为.exists():
    dlg.确认另存为..click()
# Close application
dlg.close()

你可能感兴趣的:(python学习总结)