与使用Python等程序进行交互 [英] Interact with other programs using Python

查看:432
本文介绍了与使用Python等程序进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用Python程序,该程序会发现一首歌一首抒情的名字我提供的想法。我觉得整个过程应该归结为几下面的东西。这些是我想要的程序,当我运行它做的事:

I'm having the idea of writing a program using Python which shall find a lyric of a song whose name I provided. I think the whole process should boil down to couple of things below. These are what I want the program to do when I run it:


  • 提示我输入歌曲名称

  • 复制名称

  • 打开网页浏览器(谷歌浏​​览器为例)

  • 粘贴名称在地址栏和查找有关歌曲信息

  • 打开包含歌词页

  • 复制歌词

  • 运行文本编辑器(如Microsoft Word,例如)

  • 粘贴歌词

  • 保存新的文本文件与歌曲的名称

我不要求为code,当然。我只是想知道如何使用Python与其它程序交互的概念或想法

I am not asking for code, of course. I just want to know the concepts or ideas about how to use python to interact with other programs

要更具体,我想,我想知道,狐比如,我们是多么指出哪里是谷歌浏览器的地址栏和告诉Python粘贴名称出现。或者我们如何告诉蟒蛇如何复制的歌词,以及它粘贴到Microsof Word的表,然后将其保存。

To be more specific, I think I want to know, fox example, just how we point out where is the address bar in Google Chrome and tell python to paste the name there. Or how we tell python how to copy the lyrics as well as paste it into the Microsof Word's sheet then save it.

我一直在读(我还在读书)Python的几本书:蟒蛇字节,学习Python硬盘的方式,Python的傻瓜,开始游戏开发使用Python和pygame的。然而,我发现,好像我只是(或几乎只)学会创造了自身的工作(我不能告诉我的程序做的事情,我想已经安装在我的电脑上的其他程序)程序

I've been reading (I'm still reading) several books on Python: Byte of python, Learn python the hard way, Python for dummies, Beginning Game Development with Python and Pygame. However, I found out that it seems like I only (or almost only) learn to creat programs that work on itself (I can't tell my program to do things I want with other programs that are already installed on my computer)

我知道我的问题在某种程度上听起来很傻,但我真的想知道它是如何工作的,我们告诉Python regconize,谷歌Chrome浏览器,这部分地址栏的方式,它应该贴的名字这首歌在里面。使蟒蛇互动与另一个程序的整体思路是真的很含糊,我,我只是
极想抓住。

I know that my question somehow sounds rather silly, but I really want to know how it works, the way we tell Python to regconize that this part of the Google chrome browser is the address bar and that it should paste the name of the song in it. The whole idea of making python interact with another program is really really vague to me and I just extremely want to grasp that.

谢谢大家,谁花时间阅读我这么长的问题。

Thank you everyone, whoever spend their time reading my so-long question.

ttriet204

ttriet204

推荐答案

如果你真正寻找到一个很好的借口,教自己如何与其他应用程序进行互动,这可能不是最好的之一。 Web浏览器凌乱,时间将是联合国predictable等,所以,你已经采取了一个非常艰巨的任务,一,如果你这样做,是通常的方法(谈话服务器时会很容易直接,直接创建文本文件等,都没有碰任何其他程序)。

If what you're really looking into is a good excuse to teach yourself how to interact with other apps, this may not be the best one. Web browsers are messy, the timing is going to be unpredictable, etc. So, you've taken on a very hard task—and one that would be very easy if you did it the usual way (talk to the server directly, create the text file directly, etc., all without touching any other programs).

但是,如果你想与其他应用程序进行互动,有各种不同的方法,并且是适合取决于你需要处理的各种应用程序。

But if you do want to interact with other apps, there are a variety of different approaches, and which is appropriate depends on the kinds of apps you need to deal with.


  • 一些应用程序被设计成从外面自动化。在Windows中,这几乎总是意味着他们一个COM接口,通常用一个IDispatch接口,您可以使用 pywin32 的COM包装;在Mac上,它意味着一个的AppleEvent接口,使用该 ScriptingBridge appscript ;在其他平台上也没有统一的标准。 IE浏览器(但可能不会铬)和Word都有这样的接口。

  • Some apps are designed to be automated from the outside. On Windows, this nearly always means they a COM interface, usually with an IDispatch interface, for which you can use pywin32's COM wrappers; on Mac, it means an AppleEvent interface, for which you use ScriptingBridge or appscript; on other platforms there is no universal standard. IE (but probably not Chrome) and Word both have such interfaces.

有些应用程序有一个非GUI界面,不管是你可以用的popen 驱动命令行或DLL / SO / DYLIB可以加载通过 ctypes的。或者说,理想情况下,别人已经编写Python绑定你。

Some apps have a non-GUI interface—whether that's a command line you can drive with popen, or a DLL/SO/DYLIB you can load up through ctypes. Or, ideally, someone else has already written Python bindings for you.

有些应用程序没有什么,但图形用户界面,而且也没有办法解决做GUI自动化。您可以在一个较低的水平,或者略高的水平做到这一点,通过制定WM_消息通过 pywin32 在Windows上发送​​,使用的是Mac等辅助功能的API图书馆像 pywinauto ,或可能在非常高的水平或内置自动特定的应用程序类似的工具。

Some apps have nothing but the GUI, and there's no way around doing GUI automation. You can do this at a low level, by crafting WM_ messages to send via pywin32 on Windows, using the accessibility APIs on Mac, etc., or at a somewhat higher level with libraries like pywinauto, or possibly at the very high level of selenium or similar tools built to automate specific apps.

所以,你可以从任何硒为Chrome和COM自动化的Word做到这一点,以各具特色所有自己WM_消息。如果这意味着是一个学习锻炼,问题是你想了解今天的这些事情。

So, you could do this with anything from selenium for Chrome and COM automation for Word, to crafting all the WM_ messages yourself. If this is meant to be a learning exercise, the question is which of those things you want to learn today.

让我们开始与COM自动化。使用 pywin32 ,您可以直接访问应用程序自己的脚本接口,而不必采取从用户的GUI控制,弄清楚如何导航菜单和对话框等,这是写字的宏诚宏可以是外部的脚本,而不是内部的Word的现代版,而他们不必须用VB写的,但它们看起来类似pretty。脚本的最后一部分会是这个样子:

Let's start with COM automation. Using pywin32, you directly access the application's own scripting interfaces, without having to take control of the GUI from the user, figure out how to navigate menus and dialog boxes, etc. This is the modern version of writing "Word macros"—the macros can be external scripts instead of inside Word, and they don't have to be written in VB, but they look pretty similar. The last part of your script would look something like this:

word = win32com.client.dispatch('Word.Application')
word.Visible = True
doc = word.Documents.Add()
doc.Selection.TypeText(my_string)
doc.SaveAs(r'C:\TestFiles\TestDoc.doc')

如果你看一下 Microsoft Word中的脚本,则可以看到一堆例子。但是,您可能会注意到他们正在用VBScript编写的。如果你看看周围的教程,他们都在为撰写的VBScript(或以上VB)。而对于大多数应用程序的文档VBScript的(或VB,.NET,甚至是低级别的COM)编写的。和所有的教程中我知道了使用Python的COM自动化,像<一个href=\"http://docs.activestate.com/activepython/2.7/pywin32/html/com/win32com/HTML/QuickStartClientCom.html\">Quick开始客户端COM和Python ,是谁的已经知道了COM自动化,只是想知道如何从Python中做到这一点的人写的。微软不断改变一切的名称,这一事实使得它更难寻找,你怎么也想不到,google搜索OLE自动化,ActiveX脚本,Windows脚本家等将有什么关系学习COM自动化?所以,我不知道该怎么推荐入门。我可以保证,这一切都那么简单,因为它从例子看起来上面,一旦你学会所有的废话,但我不知道怎么弄过去,最初的障碍。

If you look at Microsoft Word Scripts, you can see a bunch of examples. However, you may notice they're written in VBScript. And if you look around for tutorials, they're all written for VBScript (or older VB). And the documentation for most apps is written for VBScript (or VB, .NET, or even low-level COM). And all of the tutorials I know of for using COM automation from Python, like Quick Start to Client Side COM and Python, are written for people who already know about COM automation, and just want to know how to do it from Python. The fact that Microsoft keeps changing the name of everything makes it even harder to search for—how would you guess that googling for OLE automation, ActiveX scripting, Windows Scripting House, etc. would have anything to do with learning about COM automation? So, I'm not sure what to recommend for getting started. I can promise that it's all as simple as it looks from that example above, once you do learn all the nonsense, but I don't know how to get past that initial hurdle.

反正不是每个应用程序都是自动化的。有时,即使是这样,描述GUI操作(什么是用户将在屏幕上点击)比在应用程序的对象模型的思维简单。 选择第三段是很难在GUI术语来形容,但选择整个文档很简单,只需按control-A,或到编辑菜单,全选。 GUI自动化比COM自动化更难,因为你要么发送应用程序,Windows本身将重新present相同的邮件用户操作(例如,见的菜单的通知),或者更糟糕,工艺鼠标消息,如去(32,4)像素从左上角,单击,按下鼠标16个像素,再次点击说打开文件菜单,然后单击新建。

Anyway, not every application is automatable. And sometimes, even if it is, describing the GUI actions (what a user would click on the screen) is simpler than thinking in terms of the app's object model. "Select the third paragraph" is hard to describe in GUI terms, but "select the whole document" is easy—just hit control-A, or go to the Edit menu and Select All. GUI automation is much harder than COM automation, because you either have to send the app the same messages that Windows itself sends to represent your user actions (e.g., see "Menu Notifications") or, worse, craft mouse messages like "go (32, 4) pixels from the top-left corner, click, mouse down 16 pixels, click again" to say "open the File menu, then click New".

幸运的是,有喜欢的工具 pywinauto 这包这两种GUI自动化东西了,使它变得简单许多。还有像 swapy ,可以帮助工具你找出你想送什么命令。如果你不执着于Python的,也有像 的AutoIt 和工具 操作 这比使用 swapy更容易 pywinauto ,至少当你开始。走出这样,你的脚本的最后一部分可能类似于:

Fortunately, there are tools like pywinauto that wrap up both kinds of GUI automation stuff up to make it a lot simpler. And there are tools like swapy that can help you figure out what commands you want to send. If you're not wedded to Python, there are also tools like AutoIt and Actions that are even easier than using swapy and pywinauto, at least when you're getting started. Going this way, the last part of your script might look like:

word.Activate()
word.MenuSelect('File->New')
word.KeyStrokes(my_string)
word.MenuSelect('File->Save As')
word.Dialogs[-1].FindTextField('Filename').Select()
word.KeyStrokes(r'C:\TestFiles\TestDoc.doc')
word.Dialogs[-1].FindButton('OK').Click()

最后,即使所有的这些工具,Web浏览器是很难实现自动化,因为每个网页都有自己的菜单,按钮等,不属于Windows控件,但HTML。除非你想一路走下来的水平将鼠标移动12个像素,这是非常难以处理这些。这就是的由来 - 它的脚本的Web图形用户界面相同的方式, pywinauto 脚本的Windows图形用户界面。

Finally, even with all of these tools, web browsers are very hard to automate, because each web page has its own menus, buttons, etc. that aren't Windows controls, but HTML. Unless you want to go all the way down to the level of "move the mouse 12 pixels", it's very hard to deal with these. That's where selenium comes in—it scripts web GUIs the same way that pywinauto scripts Windows GUIs.

这篇关于与使用Python等程序进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆