def to_html(original_rtf, fixed_rtf): html = None from sh import unrtf with NamedTemporaryFile() as xml: xml.write(fixed_rtf) xml.flush() html = bytes(unrtf(xml.name)) for u in get_unencoded(original_rtf): html = html.replace(UNDECODED, u, 1) html = html.decode("latin-1") for match, correct in get_unencoded_unicode(original_rtf): html = html.replace(UNDECODED_UNICODE, correct, 1) return html.replace(">", ">").replace("<", "<")