2010-10-12 31 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) 
相關問題