首页 > 解决方案 > 如何通过 Python API 为 Google 电子表格中的单元格背景着色

问题描述

from pprint import pprint

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

client = gspread.authorize(creds)

for i in range (1,8):
    sheet = client.open("haniftest").sheet1  # Open the spreadhseet
    cell = sheet.cell(i,1).value
    print(cell)
    sheet2 = client.open("haniftest").worksheet('Sheet2')
    sheet2.update_cell(i,1,cell)

我想知道如何通过 Python API 为 Google 电子表格中的单元格背景着色

标签: pythonbackgroundgoogle-sheets-api

解决方案


要格式化特定单元格,您需要使用 format 函数更新工作表元素:

worksheet.format('A1:A1', { "backgroundColor": {"red": xx, "green": yy, "blue": zz}

您还可以查看 gspread-formatting 模块。

一些链接:


推荐阅读