您的位置:首页 > 编程语言 > Python开发

python自动化工具之pywinauto(零)

2017-10-07 16:05 597 查看

python自动化工具之pywinauto(零)

[TOC]

最近在看一个学习视频,视频是用屏幕录像大师录的,保存成exe格式。虽然也能播放,但是比较麻烦,只能在window平台播放,自带的播放器不能进行快进变速等。于是想把exe转成mp4等主流的视频格式。幸运的是,屏幕录像专家自带转换的功能。具体步骤可参考百度经验 EXE视频如何直接转换为MP4.

由于是一系列视频,每个视频都得这么导一次,很是麻烦,而且是重复操作。想到看能不能使用python脚本来模拟这一系列操作。

win32*模块

因为之前使用win32* 模块模拟人工操作,见之前写的两篇文章python使用win32*模块模拟人工操作——城通网盘下载器(零) , python使用win32*模块模拟人工操作——城通网盘下载器(一).

所以首先想到的是使用win32*模块进行模拟操作。

但是,事情不会总是一帆风顺。首先第一步(从主程序的菜单栏中调出EXE转MP4插件)就遇到了问题。如下图所示。




使用Spy++查看了一下,如图所示,发现菜单栏并不属于主窗体底下的子窗体,而状态栏、工具栏都属于主窗体的子窗体。





窗口的菜单就像窗口的标题栏一样,是窗口自身的一部分,不是其他窗体控件,也就没有办法用FindWindow和FindWindowEx返回句柄。所以要对菜单进行操作的话,我们需要新的函数,也就是GetMenu,GetSubMenu和GetMenuItemID,它们也都属于win32gui模块。

这些函数的具体用法可以参考 橘子一方的博客

如何利用Python和win32编程避免重复性体力劳动(二)——菜单操作:GetMenu,GetSubMenu,GetMenuItemID以及wParam的HIWORD&LOWORD

使用这些函数试了一下,发现效果并不好,可能是对window本身运行机制理解的不够深;另一方面可能是这方面的文档不多;还有就是这个模块已经停止更新好久了,坑没人填。

pywinauto

答案总比问题多。无意中搜到了这个人的网页Python AHK – Python automation package – Automation made simple

文中提到:

win32gui
I use win32gui for simple window handling such as moving and resizing. I personally prefer win32gui for short, simple tasks. There is one problem with win32gui, there is no real help file. Fortunately, there are plenty of examples out there. Google is your friend.

pywinauto
I use pywinauto for more complex window tasks. An example would be if I had to access a menu within a program (like File-New). For the most part it is enough to simply use win32gui. Please refer to the pywinauto documentation for help. If you are starting out with python automation, I recommend you to just use pywinauto.

mouse
Mouse is a small module to control the mouse. This is the most robust way I have found so far. The version I use is an extension of a module I found at stackoverflow – ctypes mouse_event. You can find more info on mouse in my blog post Mouse.py – Control your mouse in python.


为什么是pywinauto

作者在写pywinauto的时候,市面上已经有许多自动化脚本了。




关于pywinauto的特点,作者总结了几点:

- 提供面向对象的方法

- 使用python使调用更简洁

- 很方便的本地化(非英语语言的主要需求),这个之后会说到。

pywinauto安装

pywinauto正中我的痛点。pywinauto现在放在github上托管,关键是一直在更新。。地址: https://github.com/pywinauto/pywinauto ,官方文档https://pywinauto.readthedocs.io/en/latest/getting_started.html

README中的例子使用pywinauto操作notepad简单的无法想象。于是有了试一试的冲动。

打开anaconda prompt终端,使用pip安装pywinauto模块。

pip install -U pywinauto


安装完后,在终端中输入语句:

from pywinauto.application import Application


若没有出现错误,则说明该模块可以正常使用了。

好了,万事俱备,可以开始干了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息