Python 在xlsxwriter中传递一个用于条件格式设置的变量
我正在dataframe中为循环运行Python 在xlsxwriter中传递一个用于条件格式设置的变量,python,pandas,xlsxwriter,Python,Pandas,Xlsxwriter,我正在dataframe中为循环运行,我希望在输出报告中突出显示符合特定条件的行。但是,我的代码用于硬编码值(即10),但我想传递一个变量。谢谢你的帮助 df = pd.DataFrame({"ID": [10, 11, 12, 13, 14], "NAME": ['item1', 'item2', 'item3', 'item4', 'item5']}) number_rows = len(df.index) + 1 writer = pd.ExcelWriter("Report.xlsx
,我希望在输出报告中突出显示符合特定条件的行。但是,我的代码用于硬编码值(即10),但我想传递一个变量。谢谢你的帮助
df = pd.DataFrame({"ID": [10, 11, 12, 13, 14],
"NAME": ['item1', 'item2', 'item3', 'item4', 'item5']})
number_rows = len(df.index) + 1
writer = pd.ExcelWriter("Report.xlsx",engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
a = df['ID'][1]
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format("$A$1:$B$%d" % (number_rows),
{"type": "formula",
"criteria": '=INDIRECT("A"&ROW())= 10', # I want to pass variable (a) instead of '10'
"format": format1
}
)
workbook.close()
找到了使用“Value”属性添加变量的方法
df = pd.DataFrame({"ID": [10, 11, 12, 13, 14],
"Status": ['item1', 'item2', 'item3', 'item4', 'item5']})
number_rows = len(df.index) + 1
writer = pd.ExcelWriter("Report.xlsx",engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format("$A$1:$B$%d" % (number_rows),
{"type": "cell",
'criteria': '=',
"value": a, #here a is variable now
"format": format1
}
)
workbook.close()