简体   繁体   English

为什么 xlsxwriter 条件格式不起作用

[英]why the xlsxwriter conditional formatting does not work

my two conditional formatting statements are completely the same, however, the first works while the second doesn't.我的两个条件格式语句完全相同,但是,第一个有效而第二个无效。

i wonder why it happens and how to fix it?我想知道它为什么会发生以及如何解决它?

wb= writer.book
ws= writer.sheets['Sheet1']
ws.conditional_format('B2:L86', {'type':      '3_color_scale',
                                 'min_value': 0,
                                 'mid_value':50,
                                 'max_value': 100,
                                 'min_color': 'CC0000',
                                 'mid_color': 'white',
                                 'max_color': '006600'}
                                    )

ws.conditional_format('M2:M86', {'type':      '3_color_scale',
                                 'min_value': 0,

                                 'max_value': 1100,
                                 'min_color': 'CC0000',
                                 'mid_color': 'white',
                                 'max_color': '006600'})
ws.set_column(0, 13, 10)
wb.close()
writer.save()

picture is:图片是: 在此处输入图片说明

There is an issue, related or otherwise with the way the colors are specified.存在与指定颜色的方式相关或其他方面的问题。 They should be in HTML format like '#CC0000' and '#006600' .它们应该是 HTML 格式,如'#CC0000''#006600'

my two conditional formatting statements are completely the same我的两个条件格式语句完全一样

They aren't.他们不是。 The second one doesn't have a mid range value and the max value is higher.第二个没有中间值,最大值更高。 Fixing that and the color issue should give you the output you expect:解决这个问题和颜色问题应该给你你期望的输出:

import pandas as pd

df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet1')

ws = writer.sheets['Sheet1']
ws.conditional_format('B2:L86', {'type':      '3_color_scale',
                                 'min_value': 0,
                                 'mid_value': 50,
                                 'max_value': 100,
                                 'min_color': '#CC0000',
                                 'mid_color': 'white',
                                 'max_color': '#006600'})

ws.conditional_format('M2:M86', {'type':      '3_color_scale',
                                 'min_value': 0,
                                 'mid_value': 550,
                                 'max_value': 1100,
                                 'min_color': '#CC0000',
                                 'mid_color': 'white',
                                 'max_color': '#006600'})
ws.set_column(0, 13, 10)
writer.save()

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM