From 6695c85f2f6764bac41560ec8aef0e703c865541 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 15 Mar 2015 10:11:30 +0530 Subject: [PATCH] Do not insert an empty paragraph corresponding to every html file --- src/calibre/ebooks/docx/writer/from_html.py | 15 +++++++++++++++ src/calibre/ebooks/docx/writer/styles.py | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index 946c3a22d3..31ed9140f7 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -80,6 +80,13 @@ class TextRun(object): if preserve_whitespace: t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve') + def is_empty(self): + if not self.texts: + return True + if len(self.texts) == 1 and self.texts[0] == ('', False): + return True + return False + class Block(object): def __init__(self, styles_manager, html_block, style, is_first_block=False): @@ -127,6 +134,12 @@ class Block(object): for run in self.runs: run.serialize(p) + def is_empty(self): + for run in self.runs: + if not run.is_empty(): + return False + return True + class Convert(object): def __init__(self, oeb, docx): @@ -156,6 +169,8 @@ class Convert(object): self.blocks.append(b) is_first_block = False self.process_block(body, b, stylizer, ignore_tail=True) + if self.blocks and self.blocks[0].is_empty(): + del self.blocks[0] def process_block(self, html_block, docx_block, stylizer, ignore_tail=False): block_style = stylizer.style(html_block) diff --git a/src/calibre/ebooks/docx/writer/styles.py b/src/calibre/ebooks/docx/writer/styles.py index 450789bc83..99616aa136 100644 --- a/src/calibre/ebooks/docx/writer/styles.py +++ b/src/calibre/ebooks/docx/writer/styles.py @@ -367,6 +367,12 @@ class StylesManager(object): else: text_style.id = 'Text%d' % i text_style.name = 'Text %d' % i + for s in tuple(self.block_styles): + if s.id is None: + self.block_styles.pop(s) + for s in tuple(self.text_styles): + if s.id is None: + self.text_styles.pop(s) def serialize(self, styles): for style in sorted(self.block_styles, key=lambda s:(s is not self.normal_block_style, numeric_sort_key(s.id))):