Search code examples
pythonhtmlhtml-parsing

Replace headers by bold in HTML with python


I have some HTML text like this one:

'<H1 LANG="es-ES" CLASS="western" STYLE="text-indent: -0.5cm; line-height: 100%"><FONT FACE="Arial, sans-serif"><FONT SIZE=3>some_text_here</FONT></FONT></H1>'

within a larger HTML text. I want to automatically identify all such headers and change them to simple bold text:

'<B LANG="es-ES" CLASS="western" STYLE="text-indent: -0.5cm; line-height: 100%"><FONT FACE="Arial, sans-serif"><FONT SIZE=3>some_text_here</FONT></FONT></B>'

Using regular expressions are not the best because sometimes the header start and end are on different lines.


Solution

  • You can use BeautifulSoup but a easy way is use re.sub() in the following form:

       html_content = re.sub("<H\d", "<B", html_content)
       html_content = re.sub("<\/H\d>", "</B>", html_content)