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})
Related
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})
The issue :
I want to apply conditional formatting icon_set using xlsx to a column but do not get the right arrows for the right values
This is my desired output :
This is my current output:
This is my code:
writer.sheets[sheet].conditional_format('J54:K200', {'type': 'icon_set',
'icon_style': '3_arrows',
'icons': [
{'criteria': '>=', 'type': 'number', 'value': 1},
{'criteria': '>=', 'type': 'number', 'value': 0},
{'criteria': '<', 'type': 'number', 'value': -1}
]}
)
This is what I have looked at :
Besides similar questions here, this is what I have done :
I looked at Excel for the formula and compared to my own work, to start from my output, and figure out the correct rule.
The closest I got so far is that when I change my icons 'value'to 2, 1, 0 respectively, I get the 1 to have the middle orange arrow:
This tells me that my equality must be correct, yet it doesn't produce the expected result.
Thanks for any help provided!
If you eliminate the {'criteria': '>=', 'type': 'number', 'value': 0}, from your code it should work fine. I have a reproducible example of this below with the expected output.
import pandas as pd
import numpy as np
#Creating a sample dataframe for example demonstration
df = pd.DataFrame({'col1': [0, -1, 10, -2], 'col2': [-11, 0, -3, 1]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.sheets['Sheet1'].conditional_format('A2:B5', {'type': 'icon_set',
'icon_style': '3_arrows',
'icons': [
{'criteria': '>=', 'type': 'number', 'value': 0.001},
{'criteria': '<=', 'type': 'number', 'value': -0.001}
]}
)
writer.save()
Expected test.xlsx:
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,
})