using python package "xlsxwriter", I want to highlight cells in the following conditional range;
value > 1 or value <-1
However, some cells have -inf/inf values and it fill colors them too (to yellow). Is thare any way to unhighlight them?
I tried "conditional_format" function to uncolor them, but it doesn't work.
output example
format1 = workbook.add_format({'bg_color':'#FFBF00'}) #yellow
format2 = workbook.add_format({'bg_color':'#2E64FE'}) #blue
format3 = workbook.add_format({'bg_color':'#FFFFFF'}) #white
c_fold=[data.columns.get_loc(col) for col in data.columns if col.startswith("fold")]
c_fold.sort()
l=len(data)+1
worksheet.conditional_format(1,c_fold[0],l,c_fold[-1], {'type':'cell',
'criteria' : '>',
'value':1,
'format':format1,
})
worksheet.conditional_format(1,c_fold[0],l,c_fold[-1], {'type':'cell',
'criteria' : '<',
'value':-1,
'format':format2,
})
worksheet.conditional_format(1,c_fold[0],l,c_fold[-1], {'type':'text',
'criteria' : 'begins with',
'value':"-inf",
'format':format3,
})
Thanks in advance
In a lot of cases the answer to the question "how do I get this to work with XlsxWriter" is the same as the answer to the question "how do I get this to work with Excel".
If you try your example in Excel you will see that you get the same results as the XlsxWriter example. The > criteria is applies to -inf in Excel, thus it is highlighted as light orange. The fact that the following containing criteria also matches doesn't override the first matching criteria since Excel applies them in the order that the user supplies them.
The solution in Excel, and XlsxWriter, is to change the order that the rules are applied, like this:
import xlsxwriter
workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet1 = workbook.add_worksheet()
# Add some formats to use in the conditional formats.
format1 = workbook.add_format({'bg_color': '#FFBF00'})
format2 = workbook.add_format({'bg_color': '#2E64FE'})
format3 = workbook.add_format({'bg_color': '#FFFFFF'})
# Write some sample data.
worksheet1.write('A1', 2)
worksheet1.write('A2', '-inf')
worksheet1.write('A3', -2)
# Write a conditional formats over the same range.
worksheet1.conditional_format('A1:A3', {'type': 'text',
'criteria': 'begins with',
'value': "-inf",
'format': format3})
worksheet1.conditional_format('A1:A3', {'type': 'cell',
'criteria': '>',
'value': 1,
'format': format1})
worksheet1.conditional_format('A1:A3', {'type': 'cell',
'criteria': '<',
'value': 1,
'format': format2})
workbook.close()
Output:
This would solve
worksheet.conditional_format(1,c_fold[0],l,c_fold[-1], {'type':'text',
'criteria' : 'containing',
'value':"-inf",
'format':format3,
})
Related
The script below works as intended by highlighting the Excel Rows that contain keyword1 as red.
import xlsxwriter
workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet1 = workbook.add_worksheet()
# Keyword
keyword1 = 'red'
keyword2 = 'yellow'
# Add a format. Light red fill with dark red text.
red_colour = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
data = [
['grape', 'Smooth skin either green, red or black in colour'],
['banana', 'peelable yellow skin'],
['apple', 'red or green skin fruit'],
['melon', 'refreshing yellow or red flesh fruit'],
]
# Write the data.
for row, row_data in enumerate(data):
worksheet1.write_row(row + 0, 0, row_data)# (row, column, data)
worksheet1.conditional_format('A1:B4', {'type': 'text',
'criteria': 'containing',
'value': keyword1,
"format": red_colour})
workbook.close()
Output
Requirement
I need a way to extend on this script to search and highlight all rows when both keyword1 and keyword2 are found.
Therefore, in this example only row 4 should be highlighted as it contains instances of red and yellow as defined by keywords1 and keywords2 respectively.
Thanks in advance.
I'm not sure you can do this with a text condition, but you should be able to do this with a formula condition, like this:
formula = '=AND(ISNUMBER(SEARCH("red",B1)),ISNUMBER(SEARCH("yellow",B1)))'
worksheet1.conditional_format('$B$1:$B$4', {'type': 'formula',
'criteria': formula,
"format": red_colour})
You may have to play around with absolute vs. relative references or (annoyingly) loop through all the relevant cells with an absolute reference, as xlsxwriter doesn't always handle relative references for conditional formatting correctly, but something like this should work.
I'm trying to apply conditional formatting to my excel file using xlsxwriter, but not sure how to code for two conditions. After perusing the documentation I only see examples for one.
Concretely I'm trying to say when the value (H13) is greater than (H5) AND > (H6) then color green.
Below is my attempt that does not work. I believe this is simply a syntax issue.
worksheet.conditional_format('H13:H13', {'type': 'formula',
'criteria': '=H13 >= $H$5 and H13 > $H$6 ',
'format': green_bg})
In all cases like this it is best to figure out the conditional format in Excel first and then transfer it across to xlsxwriter.
Excel doesn't allow joined/union conditional format conditions like your example. Instead you would need to use something like AND(). Like this:
import xlsxwriter
workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet = workbook.add_worksheet()
green_bg = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
worksheet.write('H5', 2)
worksheet.write('H6', 5)
worksheet.write('H13', 9)
worksheet.conditional_format('H13:H13', {'type': 'formula',
'criteria': '=AND($H$13 >= $H$5, $H$13 > $H$6)',
'format': green_bg})
workbook.close()
Output:
However, the logical statement here is a little suspect. It is actually just the same as $H$13 > $H$6. Maybe you meant to say ..., $H$13 < $H$6.
Python 2.7:
I am trying to bold all the cells that contain a certain text in excel using XlsxWriter. I have stored the text in a list and used a for loop to iterate over the elements. I am not sure if I am using the correct syntax for specifying the value of the 'value' key in the conditional_format dictionary that XlsXwriter offers. The cells that contain the strings in my dictionary are not being converted into bold format.
header_format = new_wb.add_format({'bold': True})
header_list = ["Issue", "Type", "Status", "Resolution", "Summary", "Priority", "Fix Version", "Labels"]
for i in range(len(header_list)):
new_ws.conditional_format('A1:Z999', {'type': 'cell', 'criteria': 'equal to', 'value': '"header_list[i]"' , 'format': header_format})
You need to use the header strings as the values in conditional format, and they need to be double quoted (as required by Excel). You are trying to do that but your syntax is wrong. Here is a corrected version based on your example:
import xlsxwriter
new_wb = xlsxwriter.Workbook('test.xlsx')
new_ws = new_wb.add_worksheet()
header_format = new_wb.add_format({'bold': True})
header_list = ["Issue", "Type", "Status", "Resolution",
"Summary", "Priority", "Fix Version", "Labels"]
for value in header_list:
new_ws.conditional_format('A1:Z999', {'type': 'cell',
'criteria': 'equal to',
'value': '"%s"' % value,
'format': header_format})
# Write some strings to test against.
new_ws.write_column('A1', ['Foo', 'Type', 'Bar', 'Status'])
new_wb.close()
Output with the target words in bold:
I have what I feel could be a silly question but it seems I can't find the answer:
How to, while exporting to Excel, set a row/range to bold?
I know how to conditional format e.g.
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
...
worksheet.conditional_format('C2:AF41', {'type': 'cell', 'criteria': '<', 'value': 0, 'format': format1})
or even to format columns:
border_format = workbook.add_format({
'border': 1,
'align': 'center',
'font_size': 10
})
...
worksheet.set_column(0, 0, 30, border_format)
But let's say I want for row A1:A40 to be written in bold, whitout any particular criteria? I just want to set to bold or a color or any formatting a given range whatever data it may contain.
In this case, for instance, apply 'format1' to range A1:A40 whatever the values.
Does anyone know the answer?
It looks like there isn't a helper function specifically designed for this, according to github.
But Gabriel's answer to a similar question proposes a workaround, where you use two (or more) conditional formats to cover all values:
worksheet.conditional_format('A1:A40', {'type': 'cell',
'criteria': '>=', 'value': 0, 'format': format1})
worksheet.conditional_format('A1:A40', {'type': 'cell',
'criteria': '<', 'value': 0, 'format': format1})
I want to apply conditional formatting to all cells that contain +1. To avoid Excel from converting +1 to 1 I used '+1. However, the below code does not work. Any ideas why?
my_formats = {-1: '#FF0000',
"'+1": '#00FF00',}
for val, color in my_formats.items():
fmt = workbook.add_format({'font_color': color})
worksheet.conditional_format('B3:EK500', {'type': 'cell',
'criteria': '=',
'value': val,
'format': fmt})