Python - 条件格式 Google 表格 - “电子表格”对象没有属性“add_format”

问题描述

我正在尝试使用 Google 表格文件上的 Pandas 数据框值上传将条件格式应用于 Excel 文件的代码。

为此,我有以下代码:

import pygsheets
import pandas as pd
gc = pygsheets.authorize(service_file="")
wsh = gc.open_by_url('')
sheet = wsh[0]
my_formats = {}
for index,row in df.iterrows():
    if 'PC' in row['ID']:
        my_formats['"' + row['ID'] + '"'] = '#C00000'
    else:
        my_formats['"' + row['ID'] + '"'] = '#010203'
sheet.set_dataframe(df,'A2')
for val,color in my_formats.items():
    fmt = wsh.add_format({'bold':True,'font_color': color})
    wsh.conditional_format('F2:F50000',{'type': 'cell',#  'bold': True,'criteria': '=','value': val,'format': fmt})

使用 Pandas DataFrame Excel Write 我可以毫无问题地运行我的代码,但同样的方法在使用 pygsheets 时不起作用。任何人都知道如何使用 pygsheets 应用这种条件格式?

谢谢!

解决方法

请使用 wks.add_conditional_formatting 应用条件格式。请注意,由于尚未在 pypi 中发布,因此请使用暂存版本。

pip install --upgrade https://github.com/nithinmurali/pygsheets/archive/staging.zip

参见文档 here。并参阅 readme 中的示例。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...