diff --git a/src/calibre/ebooks/docx/writer/tables.py b/src/calibre/ebooks/docx/writer/tables.py index f2e40193e0..6a2f7e4c4d 100644 --- a/src/calibre/ebooks/docx/writer/tables.py +++ b/src/calibre/ebooks/docx/writer/tables.py @@ -73,7 +73,7 @@ class Cell(object): BLEVEL = 2 - def __init__(self, row, html_tag, tag_style): + def __init__(self, row, html_tag, tag_style=None): self.row = row self.table = self.row.table self.html_tag = html_tag @@ -85,7 +85,10 @@ class Cell(object): self.col_span = max(0, int(html_tag.get('colspan', 1))) except Exception: self.col_span = 1 - self.valign = {'top':'top', 'bottom':'bottom', 'middle':'center'}.get(tag_style._get('vertical-align')) + if tag_style is None: + self.valign = 'center' + else: + self.valign = {'top':'top', 'bottom':'bottom', 'middle':'center'}.get(tag_style._get('vertical-align')) self.items = [] self.width = convert_width(tag_style) self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor) @@ -204,6 +207,7 @@ class Row(object): def __init__(self, table, html_tag, tag_style=None): self.table = table self.html_tag = html_tag + self.orig_tag_style = tag_style self.cells = [] self.current_cell = None self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor) @@ -230,6 +234,8 @@ class Row(object): self.current_cell.add_block(block) def add_table(self, table): + if self.current_cell is None: + self.current_cell = Cell(self, self.html_tag, self.orig_tag_style) return self.current_cell.add_table(table) def serialize(self, parent, makeelement): @@ -244,6 +250,7 @@ class Table(object): def __init__(self, namespace, html_tag, tag_style=None): self.namespace = namespace self.html_tag = html_tag + self.orig_tag_style = tag_style self.rows = [] self.current_row = None self.width = convert_width(tag_style) @@ -324,6 +331,8 @@ class Table(object): self.current_row.add_block(block) def add_table(self, table): + if self.current_row is None: + self.current_row = Row(self, self.html_tag, self.orig_tag_style) return self.current_row.add_table(table) def serialize(self, parent):