Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change console font in Windows

Tags:

python

windows

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

like image 568
russo Avatar asked Aug 28 '10 22:08

russo


People also ask

Can I change the system font in Windows 10?

Open the “Start” menu, search for “Settings,” then click the first result. You can also press Windows+i to quickly open the Settings window. In Settings, click “Personalization,” then select “Fonts” in the left sidebar. On the right pane, find the font that you want to set as the default and click the font name.


2 Answers

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.

like image 133
Marfisa Avatar answered Sep 20 '22 10:09

Marfisa


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

like image 44
Redouane Zait Avatar answered Sep 18 '22 10:09

Redouane Zait