tags:

views:

102

answers:

5

Is there a way to change the console font in Windows in python 2.6?

I'm on Windows 7.

ie:

import os
os.console.font = 'Lucida Console'

*EDIT (posted this an answer by accident)

Some more information and questions:

I looked into the windows API: http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx

It look like it has a function for changing the console font:

SetCurrentConsoleFontEx

or at least getting information about the current font:

GetCurrentConsoleFont
GetCurrentConsoleFontEx

My next step was to find a python module that I can use the windows API. Here's one called pywin32: http://sourceforge.net/projects/pywin32/

The actual modules you import are not called pywin32, but win32api, win32net, win32console I figured this out by complete guesswork. Where's the documentation? a run on help('win32console')

DOESN'T show the mentioned font functions in there, it's just plain missing them. Am I missing something here? Where are the docs? Or where is a module that has all of the API's console functions...?

+1  A: 

you might wanna check http://pypi.python.org/pypi/colorama

redouane
That only shows how to change background/foreground color
Krzysztof Kowalczyk
+1  A: 

Probably not. In Windows console font is the property of and managed by the cmd.exe program.

As with everything, it's possible that if you reverse engineer how cmd.exe works, where it stores information about the font, how to force it to reload it etc. you might be able to do hack it (in any language) but there is no functionality provided by the system in a supported and documented way on how to do it.

Krzysztof Kowalczyk
A: 

It is impossible to change it for one session because the font setting is system-wide.

You can change the global font by changing some values in the registry, but you'll have to reboot the system.

leoluk
+1  A: 

I looked into the windows API: http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx

It look like it has a function for changing the console font:

SetCurrentConsoleFontEx

or at least getting information about the current font:

GetCurrentConsoleFont
GetCurrentConsoleFontEx

My next step was to find a python module that I can use the windows API. Here's one called pywin32: http://sourceforge.net/projects/pywin32/

The actual modules you import are not called pywin32, but win32api, win32net, win32console I figured this out by complete guesswork. Where's the documentation? a run on help('win32console')

DOESN'T show the mentioned font functions in there, it's just plain missing them. Am I missing something here? Where are the docs? Or where is a module that has all of the API's console functions...?

russo
You're wrong - there is `PyConsoleScreenBuffer.GetCurrentConsoleFont` method, as well as `SetConsoleFont` (although I haven't checked how it works). [Here](http://docs.activestate.com/activepython/2.6/pywin32/win32console.html) are the docs you can use.
cji
A: 

Well, I haven't dig deep enough to be able to choose font by name (and I doubt it is possible), but this code (provided that pywin32 is installed) seems to do something funny with it's console (must be cmd.exe, Console2 doesn't work, I don't know if it works with powershell):

[C:Users/cji]|1> import win32console
[C:Users/cji]|2> win32console.PyConsoleScreenBufferType( win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )  )
         <2> <PyConsoleScreenBuffer:19>
[C:Users/cji]|3> p = _
[C:Users/cji]|6> p.SetConsoleFont( 1 )
[C:Users/cji]|7> p.SetConsoleFont( 2 )
# and so on, to:
[C:Users/cji]|12> p.SetConsoleFont( 11 ) #this is Lucida Console, if I see correctly

Documentation says, that SetConsoleFont "is not documented on MSDN"... But, it certainly does something with current console font, so I think you should search in this direction.

Also, similar question: http://stackoverflow.com/questions/3222213/how-can-i-change-console-font

cji
SetConsoleFont seems to be changing to font size, not the fontAlso, I can only run your code in cmd.exe, i try making a script.py and it will error
russo