Search code examples
pythongoogle-sheetsconditional-formattingpygsheets

Python - Conditional Formatting Google Sheets - 'Spreadsheet' object has no attribute 'add_format'


I'm trying to upload my code that applies conditional formatting to an Excel file using pandas dataframe values on a google sheets file.

For that I have the following code:

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})

Using Pandas DataFrame Excel Write I was able to run my code without any problems but the same approach it doesn't work using pygsheets. Anyone knows how can I apply this conditional formatting using pygsheets?

Thanks!


Solution

  • Please use wks.add_conditional_formatting for applying conditional formatting. Note that since this is not yet released in pypi so use the staging version.

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

    see doc here. and also see examples in readme.