stuck in files!!

alex23 wuwei23 at
Mon Jul 9 03:45:00 CEST 2012

On Jul 6, 9:53 pm, Chirag B <chirag.b... at> wrote:
> i want to kno how to link two applications using python for eg:notepad
> txt file and some docx file. like i wat to kno how to take path of
> those to files and run them if i type something in
> notepad it has to come in wordpad whenever i run that code.

Here's an approach that opens Notepad & Word, then monitors Notepad
for any changes, updating the open Word document when they occur. This
hasn't been tested in any great depth, and I wouldn't run it anywhere
with existing instances of Notepad or Word open without testing what
happens first, but it should get you started.

For the most part this uses standard library files, although I did go
with WinGuiAuto as a quick and dirty wrapper around the win32gui
complexity. If you don't want to introduce it as a dependency, you can
extract the functionality you need:

    import os
    import time
    from win32com.client import Dispatch
    from winguiauto import (

    # conditional for main loop
    def window_is_open(hwnd):
            findTopWindow(selectionFunction=lambda ohwnd: hwnd ==
            return True
        except WinGuiAutoError:
            return False

    # start Notepad
    os.system('start notepad')
    time.sleep(0.1) # give it time to open or we don't get the handleW
    npad = findTopWindow(wantedText='Untitled - Notepad')
    ndoc = findControl(npad, wantedClass='Edit') # edit control for

    # start Word
    word = Dispatch("Word.Application")
    word.Visible = True
    wdoc = word.Documents.Add() # edit control (new document) for Word

    # update Word when Notepad changes
    MEMORY = None
    while window_is_open(npad):
        ntxt = getEditText(ndoc)
        if ntxt != MEMORY:
            wdoc.Content.Text = os.linesep.join(ntxt)
            MEMORY = ntxt

    wdoc.Close() # opens a save dialogue

Hope this helps.

More information about the Python-list mailing list