【问题标题】:Python - Conditional Formatting Google Sheets - 'Spreadsheet' object has no attribute 'add_format'Python - 条件格式谷歌表格 - '电子表格'对象没有属性'add_format'
【发布时间】:2020-12-26 11:36:39
【问题描述】:

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

谢谢!

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。

标签: python google-sheets conditional-formatting pygsheets


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2016-10-01
    • 2021-08-05
    相关资源
    最近更新 更多