import bleach from bleach.css_sanitizer import CSSSanitizer ALLOWED_ATTRIBUTES = {"table": ['width', 'style'], "tr": ['style'], "td": ['width', 'style'], "h3": ['style'], "p": ['style']} ALLOWED_TAGS = ['table', 'tr', 'td', 'p', 'h3', 'b', 'br', 'div', 'h5', 'hr', 'i', 'span', 'strong', 'u'] def sanitize_html_body(value): cleaned_value = bleach.clean(value, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, strip=True) return cleaned_value html_body = "<table style=\"margin:0;padding:0;border:0;font-family:Arial, Helvetica, sans-serif;border-collapse:collapse;border-spacing:0;width:100%;color:#000000;margin-bottom:14px\" width=\"100%\">\n <tr style=\"margin:0;padding:0;border:0;font-family:Arial, Helvetica, sans-serif\">\n <td style=\"margin:0;padding:0;border:0;font-family:Arial, Helvetica, sans-serif;vertical-align:top;text-align:center;width:60px;background:#ff5400\" width=\"60\">\n <b>\n \n </b>\n </td>\n <td style=\"margin:0;border:0;font-family:Arial, Helvetica, sans-serif;vertical-align:top;padding:18px;background:#f6f6f6\">\n <h3 style=\"margin:0;padding:0;border:0;font-family:Arial, Helvetica, sans-serif;font-size:18px;font-weight:bold;margin-bottom:6px\">\n test567 \n </h3>\n <p style=\"margin:0;padding:0;border:0;font-family:Arial, Helvetica, sans-serif;font-size:12px\">\n Tes1234 \n </p>\n </td>\n </tr>\n </table>" print(sanitize_html_body(html_body)) Im getting all the style attributes removed, is there a way to leave all the style css properties? Tried using the CSSSanitizer with different properties and didnt find out how to make it allow all styles Continue reading...