Format entire row with conditional_format using pandas xlswriter module - python

I am creating an excel report using pandas xlswriter module. Below the code-snippet for the same.
number_rows = len(df.index)
//df is dataframe
writer = pd.ExcelWriter("Report_1.xlsx",engine='xlsxwriter')
df.to_excel(writer,"report")
workbook = writer.book
worksheet = writer.sheets['report']
# Define range for the color formatting
color_range = "A2:F{}".format(number_rows+1)
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
worksheet.conditional_format(color_range, {'type': 'text',
'criteria' : 'containing',
'value': 'SUCCESS',
'format': format1})
I want to highlight a row ('bg_color': '#FFC7CE', 'font_color': '#9C0006') based on a cell value(=SUCCESS). But when I use the "conditional_format" it is applying only on that particular cell. Is there any way to apply the format to the entire row if the “criteria” matches cell value?

I've provided a fully reproducible example below that makes use of the INDIRECT() function from Excel (Link here). Please note that I've set the range as $A$1:$B$6 but you can extend this to a different range if you have more columns.
import pandas as pd
df = pd.DataFrame({"Name": ['A', 'B', 'C', 'D', 'E'],
"Status": ['SUCCESS', 'FAIL', 'SUCCESS', 'FAIL', 'FAIL']})
number_rows = len(df.index) + 1
writer = pd.ExcelWriter('Report_1.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": "formula",
"criteria": '=INDIRECT("B"&ROW())="SUCCESS"',
"format": format1
}
)
workbook.close()
with expected output in excel:

Related

conditional formatting python dataframe column values with different colors

I am trying to color values of one column in my df based on its values. I tried with only one value but I don't get any colored result. Here is my code
writer = pd.ExcelWriter('resultcolored.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet')
workbook = writer.book
worksheet = writer.sheets["Sheet"]
format = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
worksheet.conditional_format( 'AJ1:AJ10',
{ 'type': 'text',
'criteria': 'containing',
'value': 'Direct',
'format': format
}
)
writer.save()
It saves the file, but doesn't colot any of the values. Can you please help me out?
It should work as expected. Here is a working example based in your code:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': ['Foo', 'Director', 'bar', 'Directory', 'Baz']})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
# Apply a conditional format to the cell range.
worksheet.conditional_format( 'B2:B6',
{ 'type': 'text',
'criteria': 'containing',
'value': 'Direct',
'format': format
}
)
writer.save()
Output:

Pandas dataframe. Add an aditional row header merging all columns

I want to add a "second" header to my excel using pandas dataframe.
The excel has his values and header. But I want to add a new row above the header with just one column (the size of all columns header). And text centered.
Something like this:
How can I do this?
Use MultiIndex.from_product, but text is not centered:
df.columns = pd.MultiIndex.from_product([['Result'], df.columns])
EDIT:
import string
# Creating a DataFrame
df = pd.DataFrame(np.random.randn(8, 6), columns=list('ABCDEF'))
# Create a Pandas Excel writer using XlsxWriter engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')
# Create custom style
df.to_excel(writer, sheet_name='Sheet1', startrow=1, index=False)
# Get workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['Sheet1']
merge_format = workbook.add_format({'align': 'center'})
len_cols = len(df.columns)
#set merge_range by length of colums names
len_cols = len(df.columns)
worksheet.merge_range(0, 0, 0, len_cols - 1, 'Result', merge_format)
writer.save()

Applying formatting to multiple Excel sheets using Python and XLSXWriter

I have two dataframes as follows:
import pandas as pd
import numpy as np
from datetime import date
df = pd.DataFrame({'Data': [10, 22, 31, 43, 57, 99, 65, 74, 88],
'Data2':[10, 22, 31, 43, 57, 99, 65, 74, 88],
'Data3':[10, 22, 31, 43, 57, 99, 65, 74, 88]})
df2 = pd.DataFrame({'df2_Data': ['blue', 'yellow', 'purple', 'orange', 'green', 'brown', 'gray', 'white', 'red'],
'df2_Data2':['bike', 'car', 'bus', 'train', 'boat', 'truck', 'plane', 'scooter', 'skateboard'],
'df2_Data3':['chicken', 'cow', 'dog', 'crocodile', 'snake', 'pig', 'rat', 'mouse', 'monkey']})
I can export df, with the desired formatting, as a single sheet in an Excel with the following code:
today = date.today()
d2 = today.strftime("%B %d, %Y")
writer = pd.ExcelWriter('ExcelExample{}.xlsx'.format(d2), engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#38C4F1',
'font_color': 'FFFFFF',
'border': 1})
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num + 1, value, header_format)
writer.save()
Giving this output
OR, I can export both dataframes as separate sheets withno formatting using this code:
writer = pd.ExcelWriter('ExcelExample{}.xlsx'.format(d2), engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
How can I apply the formatting to all sheets either manually or recursively?
There is no easy way in the XlsxWriter Library to do this and this has been an issue since 2014. (https://github.com/jmcnamara/XlsxWriter/issues/111)
You can either go and work with the worksheet.write() approach (as you already did) or alternatively work with a helper function. I just found this library:
https://github.com/webermarcolivier/xlsxpandasformatter
The xlsxpandasformatter library provides a couple of helper function for XlsxWriter and pd.to_excel().
You might go and do something like this:
from xlsxpandasformatter import FormatedWorksheet
pd.formats.format.header_style = None
with pd.ExcelWriter("output_file.xlsx", engine="xlsxwriter", datetime_format="%B %d, %Y") as writer:
workbook = writer.book
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'text_v_align': 'top',
'fg_color': '#38C4F1',
'font_color': 'FFFFFF',
'border': 1})
all_df = [df1, df2]
sheet_num = 1
for df in all_df:
sheetname = 'Sheet' + str(sheet_num)
sheet_num += 1
df.to_excel(writer, sheet_name = sheetname , index=False)
worksheet = writer.sheets[sheetname]
formattedWorksheet = FormatedWorksheet(worksheet, workbook, df)
formattedWorksheet.format_header(headerFormat=header_format)
formattedWorksheet.MoreMethodsThatYouCanApply()
formattedWorksheet.apply_format_table()
The below code from this [post][1] allows me to achieve my aim of applying formatting to multiple sheets:
writer = pd.ExcelWriter('ExcelExample{}.xlsx'.format(d2), engine='xlsxwriter')
sheets_in_writer=['Sheet1','sheet2']
data_frame_for_writer=[df, df2]
for i,j in zip(data_frame_for_writer,sheets_in_writer):
i.to_excel(writer,j,index=False)
#(max_row, max_col) = df.shape
#column_settings = [{'header': column} for column in df.columns]
### Assign WorkBook
workbook=writer.book
# Add a header format
header_format = workbook.add_format({'bold': True,'text_wrap': True,'size':10,
'valign': 'top','fg_color': '#c7e7ff','border': 1})
### Apply same format on each sheet being saved
for i,j in zip(data_frame_for_writer,sheets_in_writer):
for col_num, value in enumerate(i.columns.values):
writer.sheets[j].set_column(0, max_col - 1, 12)
# writer.sheets[j].add_table(0, 0, max_row, max_col - 1, {'columns': column_settings,'autofilter': True})
writer.sheets[j].write(0, col_num, value, header_format)
writer.sheets[j].autofilter(0,0,0,i.shape[1]-1)
writer.sheets[j].freeze_panes(1,0)
writer.save()
[1]: https://stackoverflow.com/a/57350467/2781105
I used the following function to format the two tabs in Excel using Xlsxwriter. I have two dataframes and this worked exactly as I had intended.
def format_excel(writer):
""" Add Excel specific formatting to the workbooks for main report
"""
workbook = writer.book
worksheet = writer.sheets['Networks Selected']
worksheet.set_zoom(120)
worksheet.autofilter('$A$1:$G$1')
worksheet2 = writer.sheets['No Primary Selected']
worksheet2.set_zoom(120)
worksheet2.autofilter('$A$1:$G$1')
# Add some cell formats for columns with numbers
format1 = workbook.add_format({'num_format': '00.00%'})
format2 = workbook.add_format({'num_format': '0.00'})
format3 = workbook.add_format({'num_format': 'mm/dd/yyyy'})
# Set the column width and format. First Sheet
worksheet.set_column('A:A', 15)
worksheet.set_column('B:B', 25)
worksheet.set_column('C:E', 30)
worksheet.set_column('F:G', 15)
worksheet.set_column('H:H', 15, format3)
# Set the column width and format. Second Sheet
worksheet2.set_column('A:A', 15)
worksheet2.set_column('B:B', 25)
worksheet2.set_column('C:G', 30)
worksheet2.set_column('F:G', 15)
worksheet2.set_column('H:H', 15, format3)
I then wrote the output like this and called the function:
writer = pd.ExcelWriter(f"Networks Selected List v2.xlsx", engine='xlsxwriter')
df_primary.to_excel(writer, sheet_name='Networks Selected', index = False)
df_no_primary.to_excel(writer, sheet_name='No Primary Selected', index = False)
format_excel(writer)
writer.save()
create worksheets ws1 = writer.sheets['Sheet1'] and up to how many you need. Then create a dictionary for each df and corresponding ws to iterate:
wb = {'ws1': df1, 'ws2': df2, 'ws3': df3 }
for ws, df in wb.items():
ws.set_zoom(90)
ws.freeze_panes(1, 0)
for col_num, value in enumerate(df.columns.values):
ws.write(0, col_num + 1, value, header_format)

How to apply Format object to index values in pandas

Question: I have a data frame with column names with respective values. But when i apply format object to column headings, they are not responding.
Code:
import pandas as pd
root = "C:\Users\543904\Desktop\New folder\"
dict = {'name':["aparna", "pankaj"],
'degree': ["MBA", "BCA"],
'score':[90, 40]}
df = pd.DataFrame(dict)
writer = pd.ExcelWriter(root + 'output', engine = "xlsxwriter")
df.to_excel(writer, sheet_name='df', index = False)
workbook = writer.book
worksheet = writer.sheets['df']
Format_Object = workbook.add_format({'text_wrap': True})
Format_Object.set_bold()
Format_Object.set_align('center')
Format_Object.set_align('top')
Format_Object.set_border(1)
Format_Object.set_bg_color('#0ef0ce')
worksheet.set_row(0, 20, Format_Object)
writer.save()
expected:
Expected
Actual:
Actual
This is explained in the XlsxWriter docs on Working with Python Pandas and XlsxWriter:
Pandas writes the dataframe header with a default cell format. Since it is a cell format it cannot be overridden using set_row(). If you wish to use your own format for the headings then the best approach is to turn off the automatic header from Pandas and write your own. For example:
# Turn off the default header and skip one row to allow us to insert a
# user defined header.
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a header format.
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC',
'border': 1})
# Write the column headers with the defined format.
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num + 1, value, header_format)

painting a cell in excel with condition using python

I am creating an excel report that should give me a result of automatic tests. It should say if they failed/ passed.
I have created the excel report from csv using this code:
import pandas as pd
import string
writer = pd.ExcelWriter("file.xlsx", engine="xlsxwriter")
df = pd.read_csv("K:\\results.csv")
df.to_excel(writer, sheet_name=os.path.basename("K:\\results.csv"))
# skip 2 rows
df.to_excel(writer, sheet_name='Sheet1', startrow=2, header=False, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a header format.
header_format = workbook.add_format({
'bold': True,
'fg_color': '#ffcccc',
'border': 1})
# create dictionary for map length of columns
d = dict(zip(range(25), string.ascii_uppercase))
print (d)
max_len = d[len(df.columns) - 1]
print(max_len)
# C
# dynamically set merged columns in first row
worksheet.merge_range('A1:' + max_len + '1', 'This Sheet is for Personal Details')
for col_num, value in enumerate(df.columns.values):
# write to second row
worksheet.write(1, col_num, value, header_format)
column_len = df[value].astype(str).str.len().max()
column_len = max(column_len, len(value)) + 3
worksheet.set_column(col_num, col_num, column_len)
writer.save()
Now, if i have a cell that has the word" success" in it, i want to color it green, and if i have a cell in the excel which says "fail" in it i want to color it red. How can i access a specific cell in the excel file with the condition of whats written in it?
Thanks.
You could use a conditional format for this:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': ['success', 'bar', 'fail', 'foo', 'success']})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a format for fail. Light red fill with dark red text.
fail_format = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
# Add a format for pass. Green fill with dark green text.
pass_format = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
# Apply conditional formats to the cell range.
worksheet.conditional_format('B2:B6', {'type': 'text',
'criteria': 'containing',
'value': 'fail',
'format': fail_format})
worksheet.conditional_format('B2:B6', {'type': 'text',
'criteria': 'containing',
'value': 'success',
'format': pass_format})
# Close the Pandas Excel writer and output the Excel file.
writer.save()
Output:
See the XlsxWriter docs on Working with Conditional Formatting. Note, you can also use a numerical (row, col) range instead of the A1:D4 range, see the conditional_format().

Categories