Bonsoir,

Voici un code qui me permet d'envoyer un texte directement au NotePad.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding:Latin-1 -*-
 
import os
import win32con
import win32gui
 
def findControl(topHwnd,wantedText=None,wantedClass=None,selectionFunction=None):
    """
    The EnumChildWindows function enumerates the child windows that belong to the specified parent window
    by passing the handle to each child window, in turn, to an application-defined callback function.
    EnumChildWindows continues until the last child window is enumerated or the callback function returns FALSE.
    """        
    childWindows=[] # ex: childWindows ---> [(263952, '', 'Edit'), (263944, '', 'msctls_statusbar32')]
 
    try:
        win32gui.EnumChildWindows(topHwnd,_windowEnumerationHandler,childWindows)
    except:
         print"LE CONTROLE N'EST PAS UN CONTAINER! "
 
    return childWindows
 
 
def _windowEnumerationHandler(hwnd, resultList):
    '''Pass to win32gui.EnumWindows() to generate list of window handle,
    window text, window class tuples.    
    '''
 
    resultList.append((hwnd,win32gui.GetWindowText(hwnd),win32gui.GetClassName(hwnd)))
 
 
if __name__ == '__main__':
    import time
    ### 1/ Ouvrir le bloc-notes:
    os.startfile('notepad')
    time.sleep(4) 
 
    ### 2/ Récupérer le handle d'une fenêtre précise:
    BlocNote= win32gui.FindWindow(None,"Sans titre - Bloc-notes")
    print'Le handle du BlocNote est :',BlocNote
 
    ### 3/ Tous les controles:
    controls = findControl(BlocNote)
    print"Liste de tous les controls du Bloc Notes :",controls
 
    ### 4/ Envoie du texte à afficher:
 
    # J'envoie directement au widget 'Edit':
    for handle,nom,classe in controls:
        if classe=="Edit":
            win32gui.SendMessage(handle,win32con.WM_SETTEXT,0,"CE TEXTE EST ENVOYE PAR MON SCRIPT...")
J'essaie en vain d'adapter ce code pour Microsoft Word. Je ne trouve pas de control 'Edit'.
D'avance merci à qui pourra m'aider...