From e52c124224f6c72a7e36396ef138dd3e6c94a461 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 7 Feb 2022 08:19:46 +0530 Subject: [PATCH] Embed fonts: Create when missing. Fixes #1960180 [Error embedding fonts](https://bugs.launchpad.net/calibre/+bug/1960180) --- src/calibre/ebooks/oeb/polish/embed.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/oeb/polish/embed.py b/src/calibre/ebooks/oeb/polish/embed.py index c859473cd4..443933bf7c 100644 --- a/src/calibre/ebooks/oeb/polish/embed.py +++ b/src/calibre/ebooks/oeb/polish/embed.py @@ -247,7 +247,13 @@ def embed_all_fonts(container, stats, report): # Add link to CSS in all files that need it for spine_name in modified: root = container.parsed(spine_name) - head = root.xpath('//*[local-name()="head"][1]')[0] + try: + head = root.xpath('//*[local-name()="head"][1]')[0] + except IndexError: + head = root.makeelement(XHTML('head')) + root.insert(0, head) + head.tail = '\n' + head.text = '\n ' href = container.name_to_href(name, spine_name) etree.SubElement(head, XHTML('link'), rel='stylesheet', type='text/css', href=href).tail = '\n' container.dirty(spine_name)