diff --git a/src/calibre/ebooks/docx/names.py b/src/calibre/ebooks/docx/names.py index 6ff1cefa83..3d365902e7 100644 --- a/src/calibre/ebooks/docx/names.py +++ b/src/calibre/ebooks/docx/names.py @@ -7,6 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' import re +from future_builtins import map from lxml.etree import XPath as X @@ -94,3 +95,8 @@ def generate_anchor(name, existing): c += 1 return y +def children(elem, *args): + return elem.iterchildren(*map(expand, args)) + +def descendants(elem, *args): + return elem.iterdescendants(*map(expand, args)) diff --git a/src/calibre/ebooks/docx/to_html.py b/src/calibre/ebooks/docx/to_html.py index 588fff3b4c..64d1d3f795 100644 --- a/src/calibre/ebooks/docx/to_html.py +++ b/src/calibre/ebooks/docx/to_html.py @@ -14,7 +14,9 @@ from lxml.html.builder import ( HTML, HEAD, TITLE, BODY, LINK, META, P, SPAN, BR, DIV) from calibre.ebooks.docx.container import DOCX, fromstring -from calibre.ebooks.docx.names import XPath, is_tag, XML, STYLES, NUMBERING, FONTS, expand, get, generate_anchor +from calibre.ebooks.docx.names import ( + XPath, is_tag, XML, STYLES, NUMBERING, FONTS, get, generate_anchor, + descendants) from calibre.ebooks.docx.styles import Styles, inherit, PageProperties from calibre.ebooks.docx.numbering import Numbering from calibre.ebooks.docx.fonts import Fonts @@ -120,8 +122,8 @@ class Convert(object): current = [] self.page_map = OrderedDict() - for p in XPath('//w:p')(doc): - sect = XPath('descendant::w:sectPr')(p) + for p in descendants(doc, 'w:p'): + sect = tuple(descendants(p, 'w:sectPr')) if sect: pr = PageProperties(sect) for x in current + [p]: @@ -197,7 +199,7 @@ class Convert(object): current_anchor = None - for x in p.iterdescendants(expand('w:r'), expand('w:bookmarkStart')): + for x in descendants(p, 'w:r', 'w:bookmarkStart'): if x.tag.endswith('}r'): span = self.convert_run(x) if current_anchor is not None: