如何在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
相关产品推荐
相关产品推荐

