首页 > 解决方案 > Python - 条件格式谷歌表格 - '电子表格'对象没有属性'add_format'

问题描述

我正在尝试上传我的代码,该代码使用谷歌表格文件上的熊猫数据框值将条件格式应用于 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 应用这种条件格式?

谢谢!

标签: pythongoogle-sheetsconditional-formattingpygsheets

解决方案


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

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

请参阅此处的文档。并参见自述文件中的示例。


推荐阅读