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.
HTML to LaTex: How can I use python and lxml to convert an html document to LaTeX with custom macros
1 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:
result.append(el.text)
for sel in el:
if False: # get info
print('tag',sel.tag)
print('text',sel.text)
print('tail',sel.tail)
print('attrib',sel.attrib)
if sel.tag in ["h1"]:
result.append('\hmchapter{%s}' % html2latex(sel))
elif sel.tag in ["td", "table"]:
result.append("<%s>" % sel.tag)
result.append(html2latex(sel))
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)))
else:
result.append(html2latex(sel))
if sel.tail:
result.append(sel.tail)
return "".join(result)
def main():
# must be unicode or lxml parse crashes
html = u'''
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body >
<h1 class="hmchapter" data-hmvarbodychaptertitle = "My title">My title</h1>
text <span style="font-style:italic">in a specific context</span> and more.
</body>
</html>
'''
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__':
main()
which prints:
\hmchapter{My title} text \textit{in a specific context} and more.
-
Thank you so much for this answer! It is exactly what I have searched for. An elegant and minimal piece of code, easily extensible for full control over the parsing.– bjrneCommented Aug 10, 2022 at 12:24