在windows下Python打印彩色字体的方法
作者:大囚长 时间:2023-05-19 07:03:32
本文讲述了Python在windows下打印彩色字体的方法。分享给大家供大家参考,具体如下:
#################################################################
import ctypes
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
FOREGROUND_BLACK = 0x0
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN = 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN = 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.
class Color:
''''''' See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
for information on Windows APIs.'''
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def set_cmd_color(self, color, handle=std_out_handle):
"""(color) -> bit
Example: set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
"""
bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return bool
def reset_color(self):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
def print_red_text(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_green_text(self, print_text):
self.set_cmd_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_blue_text(self, print_text):
self.set_cmd_color(FOREGROUND_BLUE | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_red_text_with_blue_bg(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
print print_text
self.reset_color()
clr = Color()
# clr.print_red_text('red')
# clr.print_green_text('green')
# clr.print_blue_text('blue')
# clr.print_red_text_with_blue_bg('background')
#################################################################
来源:https://blog.csdn.net/jailman/article/details/77573941
标签:python字体,python彩色字体
0
投稿
猜你喜欢
Python+Requests+PyTest+Excel+Allure 接口自动化测试实战
2023-10-24 20:17:48
Python 描述符(Descriptor)入门
2022-10-15 21:06:59
Python读取mat文件,并保存为pickle格式的方法
2023-02-15 19:34:41
python决策树之CART分类回归树详解
2021-06-22 19:23:16
pytorch tensor计算三通道均值方式
2022-06-26 00:02:41
vue基于input实现密码的显示与隐藏功能
2024-05-09 15:28:20
python调用cmd复制文件代码分享
2022-12-26 11:18:22
MySQL查询优化之索引的应用详解
2024-01-12 14:14:38
vue动态设置页面title的方法实例
2023-07-02 16:38:56
CentOS 6.5下安装Python 3.5.2(与Python2并存)
2021-06-24 04:33:54
利用不同样式改变相同xhtml结构的布局
2008-08-20 18:17:00
Python数据分析之Matplotlib数据可视化
2022-03-05 06:37:48
学以致用驳ASP低能论
2007-08-22 14:47:00
Go编译原理之函数内联
2024-05-22 10:12:38
基于Swoole实现PHP与websocket聊天室
2023-11-03 19:04:05
python中defaultdict用法实例详解
2022-08-09 17:01:10
PHP遍历目录实现方法介绍
2023-05-25 06:31:41
JavaScript 使用技巧精萃(.net html
2023-07-02 05:18:45
flask上传作品之dbm操作的实现
2022-06-29 15:25:41
PDO::getAttribute讲解
2023-06-06 02:58:22