1

I am building a diff between two dataframes and want to save them into excel (inspired by a great answer here):

def highlight_diff(data, color='orange'):
    attr = 'background-color: {}'.format(color)
    other = data.xs('Original', axis='columns', level=-1)
    return pd.DataFrame(np.where(data.ne(other, level=0), attr, ''),
                    index=data.index, columns=data.columns)

At this point, if I look at data_swapped.style.apply(highlight_diff, axis=None) in JupyterLab, it's going to be looking great - the different columns in the second set have a background.

When you try to save this to excel, it's going to fail:

writer = pd.ExcelWriter('diff.xls')
data_swapped.style.apply(highlight_diff, axis=None).to_excel(writer, 'Sheet1')
writer.save()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/xlwt/Style.py in _parse_strg_to_obj(strg, obj, parse_dict, field_sep, line_sep, intro_sep, esc_char, debug) 636 result = parse_dict.get(section) 637 if result is None: --> 638 raise EasyXFCallerError('section %r is unknown' % section) 639 if isinstance(result, dict): 640 break

EasyXFCallerError: section 'fill' is unknown

I kind of figured that xlwt uses a different conversion for styling tags and color names and thought I'd cheat it:

attr = 'fore_color: light_yellow'

It wold remove the exception but the generated excel does not have the styling I am looking for. Is this styling approach reusable for excel at all?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
abolotnov
  • 4,282
  • 9
  • 56
  • 88

0 Answers0