Python条件格式Google Sheets“电子表格”对象没有属性“add\u format”

2024-06-16 10:21:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试上载我的代码,该代码使用google sheets文件上的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应用此条件格式吗

谢谢


Tags: 文件代码inidformatpandasmy格式
1条回答
网友
1楼 · 发布于 2024-06-16 10:21:30

请使用wks.add_conditional_formatting应用条件格式。请注意,由于这还没有在pypi中发布,所以请使用暂存版本

pip安装升级https://github.com/nithinmurali/pygsheets/archive/staging.zip

见文件here。另请参见readme中的示例

相关问题 更多 >