2010-10-12 32 views

回答

1

记事本没有从外部来源这样做的设施。没有连接到Windows窗口API,找到文本区域,并自己填充它。

+0

这意味着在python中没有任何os命令会发生这种情况? – Manoj 2010-10-12 12:27:49

+0

Preet发布了,并深入探索找到文本控件并手动填充它,没有没有。 – jdmichal 2010-10-12 12:28:55

0

编号记事本不会从标准输入读取数据,所以传递文件或操作系统级文件就是它显示文本的唯一方法。

2

有一个例子here

#### Script to try to write something down in notepad 
import win32api 
import win32gui 
import win32con 
import time 

import subprocess 
#start notepad.exe asynchronously 
subprocess.Popen('Notepad.exe') 


# get the window handle of the blank, minimized notepad window 
hwnd = win32gui.FindWindowEx(0, 0, 0, "Untitled - Notepad") 

# print it just for kicks 
print hwnd 

win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) 
#this restores the proper window, so we know we have correct handle 

#just to give it a little pause 
time.sleep(2) 

print "trying to post message" 

#try to send it a return key 
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) 
win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) 

#the above generates absolutely no effect on the notepad window. 
#same effect no matter what vk code i use (e.g. 65 for A, VK_SPACE for space, etc) 

#### end of script 
1

我可以建议你使用AutoIt3设施(http://www.autoitscript.com/autoit3/docs/tutorials/notepad/notepad.htm "AutoIt Notepad Tutorial"

AutoIt3是Windows脚本语言来控制Windows中相当事情。它提供了一个COM API,所以你可以把它在你的Python脚本集成

from win32com.client import Dispatch 
AutoIt = Dispatch("AutoItX3.Control") 
AutoIt.Run('Notepad.exe') 
AutoIt.WinWaitActive("Untitled - Notepad") 
AutoIt.Send("This is some text.") 

这可能是也可能使用的AutoHotkey(的AutoIt的完全版本的GPL)

+0

我使用紧密相关的[AutoHotkey](http://www.autohotkey.com)脚本语言和Windows剪贴板来完成类似的事情 - 不涉及Python。这使我非常怀疑有一些方法可以使用[PyWin32](http://sourceforge.net/projects/pywin32)模块来完成。 – martineau 2010-10-12 22:04:00

+0

[SendKeys](http://www.rutherfurd.net/python/sendkeys)用于Windows的Python模块听起来像可以将按键发送到记事本。 – martineau 2010-10-12 22:17:37

0

该代码将发送s到记事本窗口来自Python脚本。

class cls_KeyBdInput(ct.Structure): 
    _fields_ = [ 
     ("wVk", ct.c_ushort), 
     ("wScan", ct.c_ushort), 
     ("dwFlags", ct.c_ulong), 
     ("time", ct.c_ulong), 
     ("dwExtraInfo", ct.POINTER(ct.c_ulong)) 
    ] 
class cls_HardwareInput(ct.Structure): 
    _fields_ = [ 
     ("uMsg", ct.c_ulong), 
     ("wParamL", ct.c_short), 
     ("wParamH", ct.c_ushort) 
    ] 

class cls_MouseInput(ct.Structure): 
    _fields_ = [ 
     ("dx", ct.c_long), 
     ("dy", ct.c_long), 
     ("mouseData", ct.c_ulong), 
     ("dwFlags", ct.c_ulong), 
     ("time", ct.c_ulong), 
     ("dwExtraInfo", ct.POINTER(ct.c_ulong)) 
    ] 
class cls_Input_I(ct.Union): 
    _fields_ = [ 
     ("ki", cls_KeyBdInput), 
     ("mi", cls_MouseInput), 
     ("hi", cls_HardwareInput) 
    ] 
class cls_Input(ct.Structure): 
    _fields_ = [ 
     ("type", ct.c_ulong), 
     ("ii", cls_Input_I) 
    ] 
def make_input_objects(l_keys): 

    p_ExtraInfo_0 = ct.pointer(ct.c_ulong(0)) 

    l_inputs = [ ] 
    for n_key, n_updown in l_keys: 
     ki = cls_KeyBdInput(n_key, 0, n_updown, 0, p_ExtraInfo_0) 
     ii = cls_Input_I() 
     ii.ki = ki 
     l_inputs.append(ii) 

    n_inputs = len(l_inputs) 

    l_inputs_2=[] 
    for ndx in range(0, n_inputs): 
     s2 = "(1, l_inputs[%s])" % ndx 
     l_inputs_2.append(s2) 
    s_inputs = ', '.join(l_inputs_2) 


    cls_input_array = cls_Input * n_inputs 
    o_input_array = eval("cls_input_array(%s)" % s_inputs) 

    p_input_array = ct.pointer(o_input_array) 
    n_size_0 = ct.sizeof(o_input_array[0]) 

    # these are the args for user32.SendInput() 
    return (n_inputs, p_input_array, n_size_0) 
def send_s(window1): 
    t_s = ((0x53, 0),) 
    l_keys = [ ] 
    l_keys.extend(t_s) 
    t_inputs = make_input_objects(l_s) 
    win32gui.ShowWindow(window1, win32con.SW_SHOWNORMAL) 
    win32gui.SetForegroundWindow(window1) 
    rv = ct.windll.user32.SendInput(*t_inputs) 
def find_window(s_app_name): 

    try: 
     window1 = FindWindow( None, s_app_name,) 
     return window1 
    except ui_err: 
     pass 
    except: 
     raise 

    try: 
     window1 = FindWindow(s_app_name, None,) 
     return window1 
    except ui_err: 
     return None 
    except: 
     raise 
def search_title(srch,ttls): 
    out=None 
    for i in range(len(ttls)): 
     #print i, ttls[i][1] 
     if srch in ttls[i][1]: 
      out= ttls[i][1] 
    return out 
def get_window_titles(): 

    titles = [] 
    def foreach_window(hwnd, lParam): 
     if IsWindowVisible(hwnd):  
      length = GetWindowTextLength(hwnd) 
      buff = ctypes.create_unicode_buffer(length + 1) 
      GetWindowText(hwnd, buff, length + 1) 
      ttl=buff.value 
      titles.append((hwnd, ttl)) 
     return True 
    EnumWindows(EnumWindowsProc(foreach_window), 0) 
    return titles 

ttls=get_window_titles() 
title=search_title('Notepad',ttls) 
window1 = find_window(title) 
send_s(window1) 
相关问题