views:

49

answers:

4

I have a string called 's' and I want to open it in notepad at runtime without saving it in/as a file. Is there any way to achieve this in python?

A: 

No. Notepad does not read data from stdin, so passing it a file or OS-level file-like is the only way for it to display text.

Ignacio Vazquez-Abrams
+1  A: 

There is an example 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
Preet Sangha
A: 

Notepad has no facilities for doing such from an external source. Short of hooking into the Windows windowing API, finding the text area, and populating it yourself.

jdmichal
so that means there is not any os command in python for this to happen?
Manoj
Short of taking the answer Preet posted, and diving deeper to find the text control and manually populating it, no there is not.
jdmichal
+1  A: 

Hi, May I suggest you to use AutoIt3 facilities (http://www.autoitscript.com/autoit3/docs/tutorials/notepad/notepad.htm "AutoIt Notepad Tutorial")

AutoIt3 is a Windows scripting language to control quite anything in Windows. It provide a COM API so you can make integrate it in your Python script

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

It may be also possible to use AutoHotKey (the fully GPL version of AutoIt)

I've done something like this using the closely related [AutoHotkey](http://www.autohotkey.com) scripting language and the Windows clipboard -- no Python was involved. This make me strongly suspect there's some way to do it using the [PyWin32](http://sourceforge.net/projects/pywin32) module.
martineau
The [SendKeys](http://www.rutherfurd.net/python/sendkeys) Python module for Windows sounds like it can send keystrokes to Notepad.
martineau