You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过脚本修改Windows 11终端的字体大小或缩放比例?

可行的实现方案

1. 修改Windows Terminal配置文件

Windows Terminal的全局配置存储在settings.json中,直接修改该文件的字体大小参数是最稳定的方式:

  • 配置文件路径:%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json
  • 找到profiles.defaults.font.size字段,修改为目标数值(如12、14、16)
  • 修改后多数版本的Windows Terminal会自动加载配置,部分版本需重启终端

Python实现代码:

import json
import os

# 获取配置文件路径
config_path = os.path.expandvars(r"%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json")

def change_terminal_font_size(new_size):
    with open(config_path, 'r', encoding='utf-8') as f:
        config = json.load(f)
    
    # 修改默认配置的字体大小
    if "profiles" in config and "defaults" in config["profiles"]:
        config["profiles"]["defaults"]["font"]["size"] = new_size
    
    with open(config_path, 'w', encoding='utf-8') as f:
        json.dump(config, f, indent=2)

# 示例:将字体大小设置为14
change_terminal_font_size(14)

2. 用命令行参数启动指定字体大小的终端

如果无需修改当前终端实例,可通过wt命令的--font-size参数启动新的终端窗口:

import subprocess

def launch_terminal_with_font_size(size):
    subprocess.run(["wt", f"--font-size={size}"])

# 示例:启动字体大小为16的终端
launch_terminal_with_font_size(16)

3. 修复pynput模拟按键逻辑

你之前的pynput代码存在语法错误和快捷键匹配错误,修正后可模拟Windows Terminal的默认缩放快捷键(放大:Ctrl+Shift+=,缩小:Ctrl+-):

from pynput.keyboard import Controller, Key
import os

def get_terminal_size():
    size = os.get_terminal_size()
    return size.columns, size.lines

keyboard = Controller()

while True:
    x, y = get_terminal_size()
    print(f"当前终端尺寸: {x}x{y}")
    ch = input("放大/缩小字体(+/-,回车退出): ")
    if ch == "+":
        # 模拟Ctrl+Shift+=实现放大
        with keyboard.pressed(Key.ctrl), keyboard.pressed(Key.shift):
            keyboard.press('=')
            keyboard.release('=')
    elif ch == "-":
        # 模拟Ctrl+-实现缩小
        with keyboard.pressed(Key.ctrl):
            keyboard.press('-')
            keyboard.release('-')
    elif ch == "":
        break
    else:
        print("输入无效,请输入+/-或回车")

关于ANSI转义码无效的说明

Windows Terminal不支持通过ANSI转义码修改全局字体大小,ANSI转义码仅能控制文本的局部样式(如颜色、加粗),无法更改终端的全局缩放或字体配置。


内容的提问来源于stack exchange,提问作者Stardaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 00:26:20