【发布时间】:2011-03-04 07:44:21
【问题描述】:
可能重复:
How do I copy a string to the clipboard on Windows using Python?
谁能给我举个例子或向我解释如何使用 Python 将某些内容粘贴到活动窗口?
【问题讨论】:
可能重复:
How do I copy a string to the clipboard on Windows using Python?
谁能给我举个例子或向我解释如何使用 Python 将某些内容粘贴到活动窗口?
【问题讨论】:
如果您使用SendKeys 包,这是最简单的。您可以找到适用于各种 Python 版本的 Windows 安装程序here。
最简单的用例,发送纯文本,非常简单:
import SendKeys
SendKeys.SendKeys("Hello world")
您可以做各种漂亮的事情使用键码来表示不可打印的字符:
import SendKeys
SendKeys.SendKeys("""
{LWIN}
{PAUSE .25}
r
Notepad.exe{ENTER}
{PAUSE 1}
Hello{SPACE}World!
{PAUSE 1}
%{F4}
n
""")
阅读the documentation了解完整详情。
如果出于某种原因不想引入对非标准库包的依赖,可以使用 COM do the same thing:
import win32api
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("calc")
win32api.Sleep(100)
shell.AppActivate("Calculator")
win32api.Sleep(100)
shell.SendKeys("1{+}")
win32api.Sleep(500)
shell.SendKeys("2")
win32api.Sleep(500)
shell.SendKeys("~") # ~ is the same as {ENTER}
win32api.Sleep(500)
shell.SendKeys("*3")
win32api.Sleep(500)
shell.SendKeys("~")
win32api.Sleep(2500)
【讨论】: