You can use the str.translate method and the data in python's html package to convert characters to the equivalent html entity.
To do this, str.translate
needs a dictionary that maps characters (technically the character's integer representation, or ordinal) to html entities.
html.entities.codepoint2name
contains the required data, but the entity names are not bounded by '&' and ';'. You can use a dict comprehension to create a table with the values you need.
Once the table has been created, call your string's translate method with the table as the argument and the result will be a new string in which any characters with an html entity equivalent will have been converted.
>>> import html.entities
>>> s = 'vèlit'
>>> # Create the translation table
>>> table = {k: '&{};'.format(v) for k, v in html.entities.codepoint2name.items()}
>>> s.translate(table)
'vèlit'
>>> 'Voilà'.translate(table)
'Voilà'
Be aware that accented latin characters may be represented by a combination of unicode code points: 'è' can be represented by the single code point - LATIN SMALL LETTER E WITH GRAVE - or two codepoints - LATIN SMALL LETTER E followed by COMBINING GRAVE ACCENT. In the latter case (known as the decomposed form), the translation will not work as expected.
To get around this, you can convert the two-codepoint decomposed form to the single codepoint composed form using the normalize function from the unicodedata module in Python's standard library.
>>> decomposed
'vèlit'
>>> decomposed == s
False
>>> len(decomposed) # decomposed is longer than composed
6
>>> decomposed.translate(table)
'vèlit'
>>> composed = unicodedata.normalize('NFC', decomposed)
>>> composed == s
True
>>> composed.translate(table)
'vèlit'