本文整理汇总了Python中calibre.ebooks.docx.styles.Styles.resolve_paragraph方法的典型用法代码示例。如果您正苦于以下问题:Python Styles.resolve_paragraph方法的具体用法?Python Styles.resolve_paragraph怎么用?Python Styles.resolve_paragraph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.ebooks.docx.styles.Styles
的用法示例。
在下文中一共展示了Styles.resolve_paragraph方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Convert
# 需要导入模块: from calibre.ebooks.docx.styles import Styles [as 别名]
# 或者: from calibre.ebooks.docx.styles.Styles import resolve_paragraph [as 别名]
#.........这里部分代码省略.........
item.media_type = guess_type('a.xhtml')[0]
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
if os.path.getsize(toc_file) == 0:
os.remove(toc_file)
return os.path.join(self.dest_dir, 'metadata.opf')
def read_block_anchors(self, doc):
doc_anchors = frozenset(XPath('./w:body/w:bookmarkStart[@w:name]')(doc))
if doc_anchors:
current_bm = set()
rmap = {v:k for k, v in self.object_map.iteritems()}
for p in descendants(doc, 'w:p', 'w:bookmarkStart[@w:name]'):
if p.tag.endswith('}p'):
if current_bm and p in rmap:
para = rmap[p]
if 'id' not in para.attrib:
para.set('id', generate_anchor(next(iter(current_bm)), frozenset(self.anchor_map.itervalues())))
for name in current_bm:
self.anchor_map[name] = para.get('id')
current_bm = set()
elif p in doc_anchors:
anchor = get(p, 'w:name')
if anchor:
current_bm.add(anchor)
def convert_p(self, p):
dest = P()
self.object_map[dest] = p
style = self.styles.resolve_paragraph(p)
self.layers[p] = []
self.add_frame(dest, style.frame)
current_anchor = None
current_hyperlink = None
hl_xpath = XPath('ancestor::w:hyperlink[1]')
def p_parent(x):
# Ensure that nested <w:p> tags are handled. These can occur if a
# textbox is present inside a paragraph.
while True:
x = x.getparent()
try:
if x.tag.endswith('}p'):
return x
except AttributeError:
break
for x in descendants(p, 'w:r', 'w:bookmarkStart', 'w:hyperlink'):
if p_parent(x) is not p:
continue
if x.tag.endswith('}r'):
span = self.convert_run(x)
if current_anchor is not None:
(dest if len(dest) == 0 else span).set('id', current_anchor)
current_anchor = None
if current_hyperlink is not None:
try:
hl = hl_xpath(x)[0]
self.link_map[hl].append(span)
self.link_source_map[hl] = self.current_rels
x.set('is-link', '1')
示例2: Convert
# 需要导入模块: from calibre.ebooks.docx.styles import Styles [as 别名]
# 或者: from calibre.ebooks.docx.styles.Styles import resolve_paragraph [as 别名]
#.........这里部分代码省略.........
elem_id = ensure_id(item)
text = elem_to_toc_text(item)
toc = parent.add_item("index.html", elem_id, text)
level_prev[lvl] = toc
for i in xrange(lvl + 1, len(xpaths) + 1):
level_prev[i] = None
if len(tuple(tocroot.flat())) > 1:
return tocroot
def write(self):
toc = self.create_toc()
raw = html.tostring(self.html, encoding="utf-8", doctype="<!DOCTYPE html>")
with open(os.path.join(self.dest_dir, "index.html"), "wb") as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx)
if css:
with open(os.path.join(self.dest_dir, "docx.css"), "wb") as f:
f.write(css.encode("utf-8"))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
opf.create_spine(["index.html"])
with open(os.path.join(self.dest_dir, "metadata.opf"), "wb") as of, open(
os.path.join(self.dest_dir, "toc.ncx"), "wb"
) as ncx:
opf.render(of, ncx, "toc.ncx")
return os.path.join(self.dest_dir, "metadata.opf")
def convert_p(self, p):
dest = P()
self.object_map[dest] = p
style = self.styles.resolve_paragraph(p)
self.layers[p] = []
self.add_frame(dest, style.frame)
current_anchor = None
current_hyperlink = None
for x in descendants(p, "w:r", "w:bookmarkStart", "w:hyperlink"):
if x.tag.endswith("}r"):
span = self.convert_run(x)
if current_anchor is not None:
(dest if len(dest) == 0 else span).set("id", current_anchor)
current_anchor = None
if current_hyperlink is not None:
hl = ancestor(x, "w:hyperlink")
if hl is not None:
self.link_map[hl].append(span)
else:
current_hyperlink = None
dest.append(span)
self.layers[p].append(x)
elif x.tag.endswith("}bookmarkStart"):
anchor = get(x, "w:name")
if anchor and anchor not in self.anchor_map:
self.anchor_map[anchor] = current_anchor = generate_anchor(
anchor, frozenset(self.anchor_map.itervalues())
)
elif x.tag.endswith("}hyperlink"):
current_hyperlink = x
m = re.match(r"heading\s+(\d+)$", style.style_name or "", re.IGNORECASE)
if m is not None:
n = min(6, max(1, int(m.group(1))))
示例3: Convert
# 需要导入模块: from calibre.ebooks.docx.styles import Styles [as 别名]
# 或者: from calibre.ebooks.docx.styles.Styles import resolve_paragraph [as 别名]
#.........这里部分代码省略.........
parent = level_prev[plvl]
lvl = plvl + 1
elem_id = ensure_id(item)
text = elem_to_toc_text(item)
toc = parent.add_item('index.html', elem_id, text)
level_prev[lvl] = toc
for i in xrange(lvl+1, len(xpaths)+1):
level_prev[i] = None
if len(tuple(tocroot.flat())) > 1:
return tocroot
def write(self):
toc = self.create_toc()
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx)
if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
opf.toc = toc
opf.create_manifest_from_files_in([self.dest_dir])
opf.create_spine(['index.html'])
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(os.path.join(self.dest_dir, 'toc.ncx'), 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
return os.path.join(self.dest_dir, 'metadata.opf')
def convert_p(self, p):
dest = P()
self.object_map[dest] = p
style = self.styles.resolve_paragraph(p)
self.layers[p] = []
self.add_frame(dest, style.frame)
current_anchor = None
current_hyperlink = None
hl_xpath = XPath('ancestor::w:hyperlink[1]')
for x in descendants(p, 'w:r', 'w:bookmarkStart', 'w:hyperlink'):
if x.tag.endswith('}r'):
span = self.convert_run(x)
if current_anchor is not None:
(dest if len(dest) == 0 else span).set('id', current_anchor)
current_anchor = None
if current_hyperlink is not None:
try:
hl = hl_xpath(x)[0]
self.link_map[hl].append(span)
except IndexError:
current_hyperlink = None
dest.append(span)
self.layers[p].append(x)
elif x.tag.endswith('}bookmarkStart'):
anchor = get(x, 'w:name')
if anchor and anchor not in self.anchor_map:
self.anchor_map[anchor] = current_anchor = generate_anchor(anchor, frozenset(self.anchor_map.itervalues()))
elif x.tag.endswith('}hyperlink'):
current_hyperlink = x
m = re.match(r'heading\s+(\d+)$', style.style_name or '', re.IGNORECASE)
if m is not None:
n = min(6, max(1, int(m.group(1))))
dest.tag = 'h%d' % n