在 Python 中调用 AutoIt 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3301561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:20:27  来源:igfitidea点击:

Calling AutoIt Functions in Python

pythonautoit

提问by Wang Dingwei

I have seen this postmentioned there is an AutoIt3 COM version, and with it I can call AutoIt functions in Python.

我看到这篇文章提到有一个 AutoIt3 COM 版本,用它我可以在 Python 中调用 AutoIt 函数。

I couldn't find the COM version at the AutoIt website. Is it hidden somewhere? How can I get it?

我在 AutoIt 网站上找不到 COM 版本。是不是藏在某个地方?我怎么才能得到它?

采纳答案by cledoux

How to use AutoItX COM/DLL in python

如何在python中使用AutoItX COM/DLL

There are two methods for using AutoIt in Python:

在 Python 中使用 AutoIt 有两种方法:

  1. pyautoit module
  2. python for windows extentions (pywin32)
  1. pyautoit 模块
  2. 用于 Windows 扩展的 python (pywin32)

The pyautoit module will make use of the DLL while with pywin32 we can use the COM. As far as I know, there is no functional difference between the two.

pyautoit 模块将使用 DLL,而在 pywin32 中我们可以使用 COM。据我所知,两者在功能上没有区别。

Prerequisites

先决条件

  1. An installation of python.
  2. An installation of AutoIt.
  3. An installation of either pyautoitor pywin32.
  1. 安装python
  2. AutoIt的安装。
  3. 安装pyautoitpywin32

Not all AutoIt functions are available through the COM/DLL interface. To see which functions are, see the help file on AutoItX.

并非所有 AutoIt 功能都可通过 COM/DLL 接口使用。要查看哪些函数,请参阅 AutoItX 上的帮助文件。

Pyautoit

pyautoit

Install via pip or your preferred method:

通过 pip 或您喜欢的方法安装:

pip install -U pyautoit

If you get an error: WindowsError: [Error 193] %1 is not a valid Win32 applicationwhen installing pyautoit, use the 32 bit version of python. I haven't been able to get pyautoit to install using the 64 bit version of python. Of course, your mileage may vary.

如果出现错误:WindowsError: [Error 193] %1 is not a valid Win32 application安装pyautoit时,请使用32位版本的python。我无法使用 64 位版本的 python 安装 pyautoit。当然,您的里程可能会有所不同。

Import and use:

导入和使用:

import autoit

autoit.run("notepad.exe")
autoit.win_wait_active("[CLASS:Notepad]", 3)
autoit.control_send("[CLASS:Notepad]", "Edit1", "hello world{!}")
autoit.win_close("[CLASS:Notepad]")
autoit.control_click("[Class:#32770]", "Button2")

The autoit commands all use lower_case_with_underscores rather than AutoItX's preferred CamelCase. Thus ControlSend becomes control_send, WinClose becomes win_close, etc.

autoit 命令都使用lower_case_with_underscores 而不是AutoItX 首选的CamelCase。因此 ControlSend 变成 control_send,WinClose 变成 win_close,等等。

Pywin32

pywin32

Once pywin32 is installed, call AutoItX functions by:

安装 pywin32 后,通过以下方式调用 AutoItX 函数:

import win32com.client
autoit = win32com.client.Dispatch("AutoItX3.Control")

autoit.Run("NotePad.exe")
autoit.ControlClick(WINDOW, "", "[CLASSNN:TTreeView1]", "left", 1, 53, 41)

If you have trouble with this version, install everything as 32 bit and try again.

如果您在使用此版本时遇到问题,请将所有内容安装为 32 位,然后重试。

回答by ewall

AutoItX.dlland AutoItX3_x64.dllare included in the default installation, in a directory called "AutoItX". Check out the help file AutoItX.chmin that directory for more info.

AutoItX.dllAutoItX3_x64.dll包含在默认安装中,位于名为“AutoItX”的目录中。查看AutoItX.chm该目录中的帮助文件以获取更多信息。