from calibre.ebooks.oeb.base import XHTML for tag in root.iterdescendants('*'): if tag.name.endswith('}span'): cls = tag.get('class') if cls in {'bold', 'italic'}: tag.name = XHTML('b' if cls == 'bold' else 'i')