6

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'

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...?

Neuron
  • 5,141
  • 5
  • 38
  • 59
russo
  • 271
  • 2
  • 3
  • 9
  • ActiveState maintains very good documentation on windows extensions (somewhat incomplete, but better then nothing). It can be found here: http://docs.activestate.com/activepython/2.6/pywin32/win32console.html – cji Aug 29 '10 at 01:25

7 Answers7

14

It is possible to change the console font using ctypes. A minimal code example would look like this:

import ctypes

LF_FACESIZE = 32
STD_OUTPUT_HANDLE = -11

class COORD(ctypes.Structure):
    _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

class CONSOLE_FONT_INFOEX(ctypes.Structure):
    _fields_ = [("cbSize", ctypes.c_ulong),
                ("nFont", ctypes.c_ulong),
                ("dwFontSize", COORD),
                ("FontFamily", ctypes.c_uint),
                ("FontWeight", ctypes.c_uint),
                ("FaceName", ctypes.c_wchar * LF_FACESIZE)]
    
font = CONSOLE_FONT_INFOEX()
font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
font.nFont = 12
font.dwFontSize.X = 11
font.dwFontSize.Y = 18
font.FontFamily = 54
font.FontWeight = 400
font.FaceName = "Lucida Console"

handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
ctypes.windll.kernel32.SetCurrentConsoleFontEx(
        handle, ctypes.c_long(False), ctypes.pointer(font))

I also wrote a less minimal example on my homepage.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Marfisa
  • 181
  • 1
  • 4
  • **Note:** I think you should also include or link this minimalist version on your homepage by the way. – jeromej Apr 23 '13 at 05:32
  • This works, but I have no idea how or why. Could you give some explanation? O maybe just an ultimate minimalist version which just modifies the font size? – Robin Andrews Sep 14 '18 at 16:17
  • 1
    **On *64bit* this will yield *Undefined Behavior***. Check https://stackoverflow.com/questions/52336257/python-programatically-change-console-font-size/52340670#52340670. – CristiFati Sep 15 '18 at 08:20
  • @Marfisa Can you modify this snipped to work for 64bit Windows? – Jay Jun 21 '20 at 11:28
  • 1
    @jay: I explained in my other answer(s). It's *argtypes* (and *restype*). – CristiFati Jun 21 '20 at 12:39
2

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

Redouane Zait
  • 175
  • 3
  • 8
1

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
  • 3,513
  • 28
  • 28
1

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
  • 271
  • 2
  • 3
  • 9
  • 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 Aug 29 '10 at 00:59
0

If anyone reads this having the problem that setting the default font for PowerShell prompts to Lucida Console does not work, there can be several reasons (many of the related to only that particular font). I've blogged about it here: http://www.meadow.se/wordpress/setting-the-font-of-a-powershell-console-to-lucida-console-wont-work/

In short, for me it was necessary to change system locale from Swedish to English (United States) but there are several other possible solutions as well.

Hope this helps.

Neuron
  • 5,141
  • 5
  • 38
  • 59
emilast
  • 559
  • 1
  • 5
  • 11
0

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: How can I change console font?

Community
  • 1
  • 1
cji
  • 6,635
  • 2
  • 20
  • 16
  • SetConsoleFont seems to be changing to font size, not the font Also, I can only run your code in cmd.exe, i try making a script.py and it will error – russo Aug 29 '10 at 03:44
-2

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
  • 12,561
  • 6
  • 44
  • 51