Example #1
0
    def __init__(self, css):
        self.font_family = css_font_family_to_docx(css['font-family'])
        try:
            self.font_size = max(0, int(float(css['font-size']) * 2))  # stylizer normalizes all font sizes into pts
        except (ValueError, TypeError, AttributeError):
            self.font_size = None

        fw = css['font-weight']
        self.bold = fw.lower() in {'bold', 'bolder'} or int_or_zero(fw) >= 700
        self.italic = css['font-style'].lower() in {'italic', 'oblique'}
        self.color = convert_color(css['color'])
        self.background_color = convert_color(css.backgroundColor)
        td = set((css.effective_text_decoration or '').split())
        self.underline = 'underline' in td
        self.dstrike = 'line-through' in td and 'overline' in td
        self.strike = not self.dstrike and 'line-through' in td
        self.text_transform = css['text-transform']  # TODO: If lowercase or capitalize, transform the actual text
        self.caps = self.text_transform == 'uppercase'
        self.small_caps = css['font-variant'].lower() in {'small-caps', 'smallcaps'}
        self.shadow = css['text-shadow'] not in {'none', None}
        try:
            self.spacing = int(float(css['letter-spacing']) * 20)
        except (ValueError, TypeError, AttributeError):
            self.spacing = None
        self.vertical_align = css['vertical-align']
        for edge in border_edges:
            # In DOCX padding can only be a positive integer
            setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
            val = min(96, max(2, int({'thin':0.2, 'medium':1, 'thick':2}.get(css['border-%s-width' % edge], 0) * 8)))
            setattr(self, 'border_%s_width' % edge, val)
            setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]))
            setattr(self, 'border_%s_style' %  edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))

        DOCXStyle.__init__(self)
Example #2
0
    def __init__(self, namespace, css, is_parent_style=False):
        self.font_family = css_font_family_to_docx(css['font-family'])
        try:
            self.font_size = max(0, int(float(css['font-size']) * 2))  # stylizer normalizes all font sizes into pts
        except (ValueError, TypeError, AttributeError):
            self.font_size = None

        fw = css['font-weight']
        self.bold = fw.lower() in {'bold', 'bolder'} or int_or_zero(fw) >= 700
        self.italic = css['font-style'].lower() in {'italic', 'oblique'}
        self.color = convert_color(css['color'])
        self.background_color = None if is_parent_style else convert_color(css.backgroundColor)
        td = set((css.effective_text_decoration or '').split())
        self.underline = 'underline' in td
        self.dstrike = 'line-through' in td and 'overline' in td
        self.strike = not self.dstrike and 'line-through' in td
        self.text_transform = css['text-transform']  # TODO: If lowercase or capitalize, transform the actual text
        self.caps = self.text_transform == 'uppercase'
        self.small_caps = css['font-variant'].lower() in {'small-caps', 'smallcaps'}
        self.shadow = css['text-shadow'] not in {'none', None}
        try:
            self.spacing = int(float(css['letter-spacing']) * 20)
        except (ValueError, TypeError, AttributeError):
            self.spacing = None
        self.vertical_align = css['vertical-align']
        self.padding = self.border_color = self.border_width = self.border_style = None
        if not is_parent_style:
            # DOCX does not support individual borders/padding for inline content
            for edge in border_edges:
                # In DOCX padding can only be a positive integer
                try:
                    padding = max(0, int(css['padding-' + edge]))
                except ValueError:
                    padding = 0
                if self.padding is None:
                    self.padding = padding
                elif self.padding != padding:
                    self.padding = ignore
                val = css['border-%s-width' % edge]
                if not isinstance(val, (float, int, long)):
                    val = {'thin':0.2, 'medium':1, 'thick':2}.get(val, 0)
                val = min(96, max(2, int(val * 8)))
                if self.border_width is None:
                    self.border_width = val
                elif self.border_width != val:
                    self.border_width = ignore
                color = convert_color(css['border-%s-color' % edge])
                if self.border_color is None:
                    self.border_color = color
                elif self.border_color != color:
                    self.border_color = ignore
                style = LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none')
                if self.border_style is None:
                    self.border_style = style
                elif self.border_style != style:
                    self.border_style = ignore

        DOCXStyle.__init__(self, namespace)
Example #3
0
    def __init__(self, namespace, css, is_parent_style=False):
        self.font_family = css_font_family_to_docx(css['font-family'])
        try:
            self.font_size = max(0, int(float(css['font-size']) * 2))  # stylizer normalizes all font sizes into pts
        except (ValueError, TypeError, AttributeError):
            self.font_size = None

        fw = css['font-weight']
        self.bold = fw.lower() in {'bold', 'bolder'} or int_or_zero(fw) >= 700
        self.italic = css['font-style'].lower() in {'italic', 'oblique'}
        self.color = convert_color(css['color'])
        self.background_color = None if is_parent_style else convert_color(css.backgroundColor)
        td = set((css.effective_text_decoration or '').split())
        self.underline = 'underline' in td
        self.dstrike = 'line-through' in td and 'overline' in td
        self.strike = not self.dstrike and 'line-through' in td
        self.text_transform = css['text-transform']  # TODO: If lowercase or capitalize, transform the actual text
        self.caps = self.text_transform == 'uppercase'
        self.small_caps = css['font-variant'].lower() in {'small-caps', 'smallcaps'}
        self.shadow = css['text-shadow'] not in {'none', None}
        try:
            self.spacing = int(float(css['letter-spacing']) * 20)
        except (ValueError, TypeError, AttributeError):
            self.spacing = None
        self.vertical_align = css['vertical-align']
        self.padding = self.border_color = self.border_width = self.border_style = None
        if not is_parent_style:
            # DOCX does not support individual borders/padding for inline content
            for edge in border_edges:
                # In DOCX padding can only be a positive integer
                try:
                    padding = max(0, int(css['padding-' + edge]))
                except ValueError:
                    padding = 0
                if self.padding is None:
                    self.padding = padding
                elif self.padding != padding:
                    self.padding = ignore
                val = css['border-%s-width' % edge]
                if not isinstance(val, (float, int, long)):
                    val = {'thin':0.2, 'medium':1, 'thick':2}.get(val, 0)
                val = min(96, max(2, int(val * 8)))
                if self.border_width is None:
                    self.border_width = val
                elif self.border_width != val:
                    self.border_width = ignore
                color = convert_color(css['border-%s-color' % edge])
                if self.border_color is None:
                    self.border_color = color
                elif self.border_color != color:
                    self.border_color = ignore
                style = LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none')
                if self.border_style is None:
                    self.border_style = style
                elif self.border_style != style:
                    self.border_style = ignore

        DOCXStyle.__init__(self, namespace)
Example #4
0
def read_css_block_borders(self, css, store_css_style=False):
    for edge in border_edges:
        if css is None:
            setattr(self, "padding_" + edge, 0)
            setattr(self, "margin_" + edge, 0)
            setattr(self, "css_margin_" + edge, "")
            setattr(self, "border_%s_width" % edge, 2)
            setattr(self, "border_%s_color" % edge, None)
            setattr(self, "border_%s_style" % edge, "none")
            if store_css_style:
                setattr(self, "border_%s_css_style" % edge, "none")
        else:
            # In DOCX padding can only be a positive integer
            try:
                setattr(self, "padding_" + edge, max(0, int(css["padding-" + edge])))
            except ValueError:
                setattr(self, "padding_" + edge, 0)  # invalid value for padding
            # In DOCX margin must be a positive integer in twips (twentieth of a point)
            try:
                setattr(self, "margin_" + edge, max(0, int(css["margin-" + edge] * 20)))
            except ValueError:
                setattr(self, "margin_" + edge, 0)  # for e.g.: margin: auto
            setattr(self, "css_margin_" + edge, css._style.get("margin-" + edge, ""))
            val = css["border-%s-width" % edge]
            if not isinstance(val, (float, int, long)):
                val = {"thin": 0.2, "medium": 1, "thick": 2}.get(val, 0)
            val = min(96, max(2, int(val * 8)))
            setattr(self, "border_%s_width" % edge, val)
            setattr(self, "border_%s_color" % edge, convert_color(css["border-%s-color" % edge]) or "auto")
            setattr(self, "border_%s_style" % edge, LINE_STYLES.get(css["border-%s-style" % edge].lower(), "none"))
            if store_css_style:
                setattr(self, "border_%s_css_style" % edge, css["border-%s-style" % edge].lower())
Example #5
0
 def __init__(self, row, html_tag, tag_style=None):
     self.row = row
     self.table = self.row.table
     self.html_tag = html_tag
     try:
         self.row_span = max(0, int(html_tag.get('rowspan', 1)))
     except Exception:
         self.row_span = 1
     try:
         self.col_span = max(0, int(html_tag.get('colspan', 1)))
     except Exception:
         self.col_span = 1
     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)
     read_css_block_borders(self, tag_style)
Example #6
0
 def __init__(self, table, html_tag, tag_style=None):
     self.table = table
     self.html_tag = html_tag
     self.cells = []
     self.current_cell = None
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     read_css_block_borders(self, tag_style)
Example #7
0
def read_css_block_borders(self, css, store_css_style=False):
    for edge in border_edges:
        if css is None:
            setattr(self, 'padding_' + edge, 0)
            setattr(self, 'margin_' + edge, 0)
            setattr(self, 'css_margin_' + edge, '')
            setattr(self, 'border_%s_width' % edge, 2)
            setattr(self, 'border_%s_color' % edge, None)
            setattr(self, 'border_%s_style' %  edge, 'none')
            if store_css_style:
                setattr(self, 'border_%s_css_style' %  edge, 'none')
        else:
            # In DOCX padding can only be a positive integer
            try:
                setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
            except ValueError:
                setattr(self, 'padding_' + edge, 0)  # invalid value for padding
            # In DOCX margin must be a positive integer in twips (twentieth of a point)
            try:
                setattr(self, 'margin_' + edge, max(0, int(css['margin-' + edge] * 20)))
            except ValueError:
                setattr(self, 'margin_' + edge, 0)  # for e.g.: margin: auto
            setattr(self, 'css_margin_' + edge, css._style.get('margin-' + edge, ''))
            val = css['border-%s-width' % edge]
            if not isinstance(val, (float, int, long)):
                val = {'thin':0.2, 'medium':1, 'thick':2}.get(val, 0)
            val = min(96, max(2, int(val * 8)))
            setattr(self, 'border_%s_width' % edge, val)
            setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]) or 'auto')
            setattr(self, 'border_%s_style' %  edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
            if store_css_style:
                setattr(self, 'border_%s_css_style' %  edge, css['border-%s-style' % edge].lower())
Example #8
0
    def __init__(self, namespace, css, html_block, is_table_cell=False):
        read_css_block_borders(self, css)
        if is_table_cell:
            for edge in border_edges:
                setattr(self, 'border_%s_style' % edge, 'none')
                setattr(self, 'border_%s_width' % edge, 0)
                setattr(self, 'padding_' + edge, 0)
                setattr(self, 'margin_' + edge, 0)
        if css is None:
            self.page_break_before = self.keep_lines = False
            self.text_indent = 0
            self.css_text_indent = None
            self.line_height = 280
            self.background_color = None
            self.text_align = 'left'
        else:
            self.page_break_before = css['page-break-before'] == 'always'
            self.keep_lines = css['page-break-inside'] == 'avoid'
            self.text_indent = int(css['text-indent'] * 20)
            self.css_text_indent = css._get('text-indent')
            self.line_height = max(0, int(css.lineHeight * 20))
            self.background_color = None if is_table_cell else convert_color(css['background-color'])
            self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
                css['text-align'].lower(), 'left')

        DOCXStyle.__init__(self, namespace)
Example #9
0
    def __init__(self, namespace, css, html_block, is_table_cell=False):
        read_css_block_borders(self, css)
        if is_table_cell:
            for edge in border_edges:
                setattr(self, 'border_%s_style' % edge, 'none')
                setattr(self, 'border_%s_width' % edge, 0)
                setattr(self, 'padding_' + edge, 0)
                setattr(self, 'margin_' + edge, 0)
        if css is None:
            self.text_indent = 0
            self.css_text_indent = None
            self.line_height = 280
            self.background_color = None
            self.text_align = 'left'
        else:
            try:
                self.text_indent = int(css['text-indent'] * 20)
                self.css_text_indent = css._get('text-indent')
            except (TypeError, ValueError):
                self.text_indent = 0
                self.css_text_indent = None
            try:
                self.line_height = max(0, int(css.lineHeight * 20))
            except (TypeError, ValueError):
                self.line_height = max(0, int(1.2 * css.fontSize * 20))
            self.background_color = None if is_table_cell else convert_color(css['background-color'])
            try:
                self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
                    css['text-align'].lower(), 'left')
            except AttributeError:
                self.text_align = 'left'

        DOCXStyle.__init__(self, namespace)
Example #10
0
def read_css_block_borders(self, css, store_css_style=False):
    for edge in border_edges:
        if css is None:
            setattr(self, 'padding_' + edge, 0)
            setattr(self, 'margin_' + edge, 0)
            setattr(self, 'css_margin_' + edge, '')
            setattr(self, 'border_%s_width' % edge, 2)
            setattr(self, 'border_%s_color' % edge, None)
            setattr(self, 'border_%s_style' %  edge, 'none')
            if store_css_style:
                setattr(self, 'border_%s_css_style' %  edge, 'none')
        else:
            # In DOCX padding can only be a positive integer
            try:
                setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
            except ValueError:
                setattr(self, 'padding_' + edge, 0)  # invalid value for padding
            # In DOCX margin must be a positive integer in twips (twentieth of a point)
            try:
                setattr(self, 'margin_' + edge, max(0, int(css['margin-' + edge] * 20)))
            except ValueError:
                setattr(self, 'margin_' + edge, 0)  # for e.g.: margin: auto
            setattr(self, 'css_margin_' + edge, css._style.get('margin-' + edge, ''))
            val = css['border-%s-width' % edge]
            if not isinstance(val, (float, int, long)):
                val = {'thin':0.2, 'medium':1, 'thick':2}.get(val, 0)
            val = min(96, max(2, int(val * 8)))
            setattr(self, 'border_%s_width' % edge, val)
            setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]) or 'auto')
            setattr(self, 'border_%s_style' %  edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
            if store_css_style:
                setattr(self, 'border_%s_css_style' %  edge, css['border-%s-style' % edge].lower())
Example #11
0
    def __init__(self, namespace, css, html_block, is_table_cell=False):
        read_css_block_borders(self, css)
        if is_table_cell:
            for edge in border_edges:
                setattr(self, 'border_%s_style' % edge, 'none')
                setattr(self, 'border_%s_width' % edge, 0)
                setattr(self, 'padding_' + edge, 0)
                setattr(self, 'margin_' + edge, 0)
        if css is None:
            self.text_indent = 0
            self.css_text_indent = None
            self.line_height = 280
            self.background_color = None
            self.text_align = 'left'
        else:
            self.text_indent = int(css['text-indent'] * 20)
            self.css_text_indent = css._get('text-indent')
            try:
                self.line_height = max(0, int(css.lineHeight * 20))
            except (TypeError, ValueError):
                self.line_height = max(0, int(1.2 * css.fontSize * 20))
            self.background_color = None if is_table_cell else convert_color(css['background-color'])
            try:
                self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
                    css['text-align'].lower(), 'left')
            except AttributeError:
                self.text_align = 'left'

        DOCXStyle.__init__(self, namespace)
Example #12
0
    def __init__(self, namespace, css, html_block, is_table_cell=False):
        read_css_block_borders(self, css)
        if is_table_cell:
            for edge in border_edges:
                setattr(self, 'border_%s_style' % edge, 'none')
                setattr(self, 'border_%s_width' % edge, 0)
                setattr(self, 'padding_' + edge, 0)
                setattr(self, 'margin_' + edge, 0)
        if css is None:
            self.page_break_before = self.keep_lines = False
            self.text_indent = 0
            self.css_text_indent = None
            self.line_height = 280
            self.background_color = None
            self.text_align = 'left'
        else:
            self.page_break_before = css['page-break-before'] == 'always'
            self.keep_lines = css['page-break-inside'] == 'avoid'
            self.text_indent = max(0, int(css['text-indent'] * 20))
            self.css_text_indent = css._get('text-indent')
            self.line_height = max(0, int(css.lineHeight * 20))
            self.background_color = None if is_table_cell else convert_color(
                css['background-color'])
            self.text_align = {
                'start': 'left',
                'left': 'left',
                'end': 'right',
                'right': 'right',
                'center': 'center',
                'justify': 'both',
                'centre': 'center'
            }.get(css['text-align'].lower(), 'left')

        DOCXStyle.__init__(self, namespace)
Example #13
0
 def __init__(self, row, html_tag, tag_style):
     self.row = row
     self.table = self.row.table
     self.html_tag = html_tag
     self.items = []
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     read_css_block_borders(self, tag_style)
Example #14
0
 def __init__(self, table, html_tag, tag_style=None):
     self.table = table
     self.html_tag = html_tag
     self.cells = []
     self.current_cell = None
     self.background_color = None if tag_style is None else convert_color(
         tag_style.backgroundColor)
     read_css_block_borders(self, tag_style)
Example #15
0
 def __init__(self, row, html_tag, tag_style):
     self.row = row
     self.table = self.row.table
     self.html_tag = html_tag
     self.items = []
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(
         tag_style.backgroundColor)
     read_css_block_borders(self, tag_style)
Example #16
0
    def __init__(self, css, html_block, is_first_block=False):
        self.page_break_before = html_block.tag.endswith('}body') or (
            not is_first_block and css['page-break-before'] == 'always')
        self.keep_lines = css['page-break-inside'] == 'avoid'
        for edge in border_edges:
            # In DOCX padding can only be a positive integer
            setattr(self, 'padding_' + edge, max(0,
                                                 int(css['padding-' + edge])))
            # In DOCX margin must be a positive integer in twips (twentieth of a point)
            setattr(self, 'margin_' + edge,
                    max(0, int(css['margin-' + edge] * 20)))
            setattr(self, 'css_margin_' + edge,
                    css._style.get('margin-' + edge, ''))
            val = min(
                96,
                max(
                    2,
                    int({
                        'thin': 0.2,
                        'medium': 1,
                        'thick': 2
                    }.get(css['border-%s-width' % edge], 0) * 8)))
            setattr(self, 'border_%s_width' % edge, val)
            setattr(self, 'border_%s_color' % edge,
                    convert_color(css['border-%s-color' % edge]))
            setattr(
                self, 'border_%s_style' % edge,
                LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
        self.text_indent = max(0, int(css['text-indent'] * 20))
        self.css_text_indent = css._get('text-indent')
        self.line_height = max(0, int(css['line-height'] * 20))
        self.css_line_height = css._get('line-height')
        self.background_color = convert_color(css['background-color'])
        self.text_align = {
            'start': 'left',
            'left': 'left',
            'end': 'right',
            'right': 'right',
            'center': 'center',
            'justify': 'both',
            'centre': 'center'
        }.get(css['text-align'].lower(), 'left')

        DOCXStyle.__init__(self)
Example #17
0
    def __init__(self, css, html_block, is_first_block=False):
        self.page_break_before = html_block.tag.endswith('}body') or (not is_first_block and css['page-break-before'] == 'always')
        self.keep_lines = css['page-break-inside'] == 'avoid'
        for edge in border_edges:
            # In DOCX padding can only be a positive integer
            setattr(self, 'padding_' + edge, max(0, int(css['padding-' + edge])))
            # In DOCX margin must be a positive integer in twips (twentieth of a point)
            setattr(self, 'margin_' + edge, max(0, int(css['margin-' + edge] * 20)))
            setattr(self, 'css_margin_' + edge, css._style.get('margin-' + edge, ''))
            val = min(96, max(2, int({'thin':0.2, 'medium':1, 'thick':2}.get(css['border-%s-width' % edge], 0) * 8)))
            setattr(self, 'border_%s_width' % edge, val)
            setattr(self, 'border_%s_color' % edge, convert_color(css['border-%s-color' % edge]))
            setattr(self, 'border_%s_style' %  edge, LINE_STYLES.get(css['border-%s-style' % edge].lower(), 'none'))
        self.text_indent = max(0, int(css['text-indent'] * 20))
        self.css_text_indent = css._get('text-indent')
        self.line_height = max(0, int(css.lineHeight * 20))
        self.background_color = convert_color(css['background-color'])
        self.text_align = {'start':'left', 'left':'left', 'end':'right', 'right':'right', 'center':'center', 'justify':'both', 'centre':'center'}.get(
            css['text-align'].lower(), 'left')

        DOCXStyle.__init__(self)
Example #18
0
 def __init__(self, html_tag, tag_style=None):
     self.html_tag = html_tag
     self.rows = []
     self.current_row = None
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     self.jc = None
     if tag_style is not None:
         ml, mr = tag_style._get('margin-left'), tag_style.get('margin-right')
         if ml == 'auto':
             self.jc = 'center' if mr == 'auto' else 'right'
     read_css_block_borders(self, tag_style)
Example #19
0
 def __init__(self, html_tag, tag_style=None):
     self.html_tag = html_tag
     self.rows = []
     self.current_row = None
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(
         tag_style.backgroundColor)
     self.jc = None
     if tag_style is not None:
         ml, mr = tag_style._get('margin-left'), tag_style.get(
             'margin-right')
         if ml == 'auto':
             self.jc = 'center' if mr == 'auto' else 'right'
     read_css_block_borders(self, tag_style)
Example #20
0
 def __init__(self, row, html_tag, tag_style):
     self.row = row
     self.table = self.row.table
     self.html_tag = html_tag
     try:
         self.row_span = max(0, int(html_tag.get('rowspan', 1)))
     except Exception:
         self.row_span = 1
     try:
         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'))
     self.items = []
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     read_css_block_borders(self, tag_style)
Example #21
0
 def __init__(self, namespace, html_tag, tag_style=None):
     self.namespace = namespace
     self.html_tag = html_tag
     self.rows = []
     self.current_row = None
     self.width = convert_width(tag_style)
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     self.jc = None
     self.float = None
     self.margin_left = self.margin_right = self.margin_top = self.margin_bottom = None
     if tag_style is not None:
         ml, mr = tag_style._get('margin-left'), tag_style.get('margin-right')
         if ml == 'auto':
             self.jc = 'center' if mr == 'auto' else 'right'
         self.float = tag_style['float']
         for edge in border_edges:
             setattr(self, 'margin_' + edge, tag_style['margin-' + edge])
     read_css_block_borders(self, tag_style)
Example #22
0
 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)
     self.background_color = None if tag_style is None else convert_color(tag_style.backgroundColor)
     self.jc = None
     self.float = None
     self.margin_left = self.margin_right = self.margin_top = self.margin_bottom = None
     if tag_style is not None:
         ml, mr = tag_style._get('margin-left'), tag_style.get('margin-right')
         if ml == 'auto':
             self.jc = 'center' if mr == 'auto' else 'right'
         self.float = tag_style['float']
         for edge in border_edges:
             setattr(self, 'margin_' + edge, tag_style['margin-' + edge])
     read_css_block_borders(self, tag_style)
Example #23
0
    def __init__(self, namespace, css, html_block, is_table_cell=False):
        read_css_block_borders(self, css)
        if is_table_cell:
            for edge in border_edges:
                setattr(self, "border_%s_style" % edge, "none")
                setattr(self, "border_%s_width" % edge, 0)
                setattr(self, "padding_" + edge, 0)
                setattr(self, "margin_" + edge, 0)
        if css is None:
            self.text_indent = 0
            self.css_text_indent = None
            self.line_height = 280
            self.background_color = None
            self.text_align = "left"
        else:
            self.text_indent = int(css["text-indent"] * 20)
            self.css_text_indent = css._get("text-indent")
            try:
                self.line_height = max(0, int(css.lineHeight * 20))
            except (TypeError, ValueError):
                self.line_height = max(0, int(1.2 * css.fontSize * 20))
            self.background_color = None if is_table_cell else convert_color(css["background-color"])
            try:
                self.text_align = {
                    "start": "left",
                    "left": "left",
                    "end": "right",
                    "right": "right",
                    "center": "center",
                    "justify": "both",
                    "centre": "center",
                }.get(css["text-align"].lower(), "left")
            except AttributeError:
                self.text_align = "left"

        DOCXStyle.__init__(self, namespace)
Example #24
0
    def __init__(self, namespace, css, is_parent_style=False):
        self.font_family = css_font_family_to_docx(css["font-family"])
        try:
            self.font_size = max(0, int(float(css["font-size"]) * 2))  # stylizer normalizes all font sizes into pts
        except (ValueError, TypeError, AttributeError):
            self.font_size = None

        fw = css["font-weight"]
        self.bold = (fw.lower() if hasattr(fw, "lower") else fw) in {"bold", "bolder"} or int_or_zero(fw) >= 700
        self.italic = css["font-style"].lower() in {"italic", "oblique"}
        self.color = convert_color(css["color"])
        self.background_color = None if is_parent_style else convert_color(css.backgroundColor)
        td = set((css.effective_text_decoration or "").split())
        self.underline = "underline" in td
        self.dstrike = "line-through" in td and "overline" in td
        self.strike = not self.dstrike and "line-through" in td
        self.text_transform = css["text-transform"]  # TODO: If lowercase or capitalize, transform the actual text
        self.caps = self.text_transform == "uppercase"
        self.small_caps = css["font-variant"].lower() in {"small-caps", "smallcaps"}
        self.shadow = css["text-shadow"] not in {"none", None}
        try:
            self.spacing = int(float(css["letter-spacing"]) * 20)
        except (ValueError, TypeError, AttributeError):
            self.spacing = None
        va = css.first_vertical_align
        if isinstance(va, (int, float)):
            self.vertical_align = str(int(va * 2))
        else:
            val = {
                "top": "superscript",
                "text-top": "superscript",
                "sup": "superscript",
                "super": "superscript",
                "bottom": "subscript",
                "text-bottom": "subscript",
                "sub": "subscript",
            }.get(va)
            self.vertical_align = val or "baseline"

        self.padding = self.border_color = self.border_width = self.border_style = None
        if not is_parent_style:
            # DOCX does not support individual borders/padding for inline content
            for edge in border_edges:
                # In DOCX padding can only be a positive integer
                try:
                    padding = max(0, int(css["padding-" + edge]))
                except ValueError:
                    padding = 0
                if self.padding is None:
                    self.padding = padding
                elif self.padding != padding:
                    self.padding = ignore
                val = css["border-%s-width" % edge]
                if not isinstance(val, (float, int, long)):
                    val = {"thin": 0.2, "medium": 1, "thick": 2}.get(val, 0)
                val = min(96, max(2, int(val * 8)))
                if self.border_width is None:
                    self.border_width = val
                elif self.border_width != val:
                    self.border_width = ignore
                color = convert_color(css["border-%s-color" % edge])
                if self.border_color is None:
                    self.border_color = color
                elif self.border_color != color:
                    self.border_color = ignore
                style = LINE_STYLES.get(css["border-%s-style" % edge].lower(), "none")
                if self.border_style is None:
                    self.border_style = style
                elif self.border_style != style:
                    self.border_style = ignore

        if self.padding in (None, ignore):
            self.padding = 0
        if self.border_width in (None, ignore):
            self.border_width = 0
        if self.border_style in (None, ignore):
            self.border_style = "none"
        if self.border_color in (None, ignore):
            self.border_color = "auto"
        if self.border_style == "none":
            self.border_width, self.border_color = 0, "auto"

        DOCXStyle.__init__(self, namespace)