A small answer tp @fraxel updates, which returns 2 outputs - the original and the new version with marked changes. I also change the single-line font to a more readable version, in my opinion.
def show_diff(text, n_text): seqm = difflib.SequenceMatcher(None, text, n_text) output_orig = [] output_new = [] for opcode, a0, a1, b0, b1 in seqm.get_opcodes(): orig_seq = seqm.a[a0:a1] new_seq = seqm.b[b0:b1] if opcode == 'equal': output_orig.append(orig_seq) output_new.append(orig_seq) elif opcode == 'insert': output_new.append("<font color=green>{}</font>".format(new_seq)) elif opcode == 'delete': output_orig.append("<font color=red>{}</font>".format(orig_seq)) elif opcode == 'replace': output_new.append("<font color=blue>{}</font>".format(new_seq)) output_orig.append("<font color=blue>{}</font>".format(orig_seq)) else: print('Error') return ''.join(output_orig), ''.join(output_new)
source share