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

如何在GSpread Python中用行列坐标而非A1标记格式化表格

GSpread 行列坐标格式化表格解决方案

依赖准备

先安装格式化扩展库:

pip install gspread-formatting

1. 设置首行前50列加粗

通过代码自动将行列坐标转换为A1范围,无需手动查找列字母:

import gspread
from gspread_formatting import format_cell_range, CellFormat, TextFormat
from gspread.utils import get_column_letter

# 授权并打开目标工作表(替换为你的授权文件和表格信息)
gc = gspread.service_account(filename='service_account.json')
sh = gc.open('你的表格名称')
worksheet = sh.sheet1

# 定义首行前50列的范围:对应(1,1)到(1,50)
row_num = 1
start_col, end_col = 1, 50
a1_range = f"{get_column_letter(start_col)}{row_num}:{get_column_letter(end_col)}{row_num}"

# 应用加粗格式
bold_format = CellFormat(textFormat=TextFormat(bold=True))
format_cell_range(worksheet, a1_range, bold_format)

2. 调整所有列宽

直接用行列索引批量设置列宽:

# 批量设置第1到50列的宽度为200像素(数值可按需调整)
worksheet.set_column_width(1, 50, 200)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 08:06:50