[Tex/LaTex] HTML to LaTex: How to use python and lxml to convert an html document to LaTeX with custom macros


I want to use python to take html5 documents with a known set of tags and convert them to LaTeX for high-quality printing using customized LaTeX macros.

Best Answer

I looked at many tools and settled on lxml with a recursive function to map html tags to LaTeX markup. It gives you a single place to easily define your mapping with Python. I believe I worked from an example in the book, Foundations of Python Network Programming

Here is a minimum working example in Python 2.7:

# convert html document to LaTeX
import lxml.html # http://lxml.de/lxmlhtml.html
from lxml import etree
from io import StringIO, BytesIO

def html2latex(el): # fill in this function to catch and convert html tags
    result = []
    if el.text:
    for sel in el:
        if False: # get info
        if sel.tag in ["h1"]:
            result.append('\hmchapter{%s}' % html2latex(sel))
        elif sel.tag in ["td", "table"]:
            result.append("<%s>" % sel.tag)
            result.append("</%s>" % sel.tag)
        elif sel.tag in ["span"]:  #
            for att in sel.attrib.keys():
                if att =='style':
                    if sel.attrib[att] == 'font-style:italic':
                        result.append(r'\textit{%s}' % (html2latex(sel)))
        if sel.tail:
    return "".join(result)

def main():
    # must be unicode or lxml parse crashes
    html = u'''
<!DOCTYPE html>
<body >
  <h1 class="hmchapter" data-hmvarbodychaptertitle = "My title">My title</h1>
  text <span style="font-style:italic">in a specific context</span> and more.
    parser = etree.HTMLParser()
    tree   = etree.parse(StringIO(html), parser) # expects a file, use StringIO for string
    root = tree.getroot()
    latex = html2latex(root)
    print latex

if __name__ == '__main__':

which prints:

\hmchapter{My title}
text \textit{in a specific context} and more.