getting text from WinXP console

Jeff Shannon jeffshannon at
Mon Mar 21 21:06:03 CET 2005

Lucas Raab wrote:

> Chris Maloof wrote:
>> Does anyone know how I can read the ASCII text from a console window
>> (from another application) in WinXP?  It doesn't sound like a major
>> operation, but although I can find the window via pywin32, I haven't
>> been able to do anything with it.  I'd really just like to get the
>> window text into a string.
>> By "console window", I mean the sort of thing that comes up when you
>> run "command" (although this particular one is for the game NetHack).
> If you're looking to do this from Python then do "python <> > 
> output.txt" only without the quotes. AFAIK, this only works on Windows. 
> Correct me if it works from Linux or another OS.

It works in *nix (and probably other OSes as well) to the same extent 
that it works in Windows.  However, it does *not* do what the OP was 
asking for -- it captures the (console-mode) output of into 
a file, but the OP (as I understand it) wanted to have 
capture the already-displayed console output of someotherapp.exe.

Unless I'm seriously mistaken, the only way that this will be possible 
is if there's a Win32 API call that will give the correct information. 
  This might be possible to find in the MSDN documentation, if it 
exists, but I suspect that it probably doesn't.

Actually, there's probably a second way -- capture the window image as 
a bitmap, and then run OCR software on it.  I'm not sure how well most 
OCR software will deal with ASCII graphics, though -- they tend to be 
optimized for reading "real" text (i.e. words in an actual language).

If it's permissible for the capturing program to start the application 
whose output is to be captured, then it may be possible to work as a 
proxy -- the capture app would pass (almost) all input to the child 
app and then retrieve the child app's output (and probably perform the 
actual display on-screen).  This won't let you capture the text of an 
arbitrary window, though, and would probably be pretty fragile.

Jeff Shannon

More information about the Python-list mailing list