From d8bfb8c8b1ee17beb0ac2144e7e6e375d574fffb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 13 Apr 2016 07:14:57 +0530 Subject: [PATCH] Conversion: Fix the obsolete HTML align=center markup (produced by Microsoft Word) not working for tables. Fixes #1569583 [table alignment lost on conversion](https://bugs.launchpad.net/calibre/+bug/1569583) --- src/calibre/ebooks/oeb/transforms/flatcss.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 35144da646..cf13540990 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -344,6 +344,20 @@ class CSSFlattener(object): if 'align' in node.attrib: if tag != 'img': cssdict['text-align'] = node.attrib['align'] + if cssdict['text-align'] == 'center': + # align=center causes tables to be center aligned, + # which text-align does not. And the ever trustworthy Word + # uses this construct in its HTML output. See + # https://bugs.launchpad.net/bugs/1569583 + if tag == 'table': + if 'margin-left' not in cssdict and 'margin-right' not in cssdict: + cssdict['margin-left'] = cssdict['margin-right'] = 'auto' + else: + for table in node.iterchildren(XHTML("table")): + ts = stylizer.style(table) + if ts.get('margin-left') is None and ts.get('margin-right') is None: + ts.set('margin-left', 'auto') + ts.set('margin-right', 'auto') else: val = node.attrib['align'] if val in ('middle', 'bottom', 'top'):