def fill_attribute_dict(self, attribute_dict: Dict[str, str]) -> Dict[str, Any]: new_attr_dict: Dict[str, Any] = deepcopy(attribute_dict) if 'align' in attribute_dict: new_attr_dict['align'] = self.str_to_align(attribute_dict['align']) if 'font_family' in attribute_dict and not font_reader.is_registered_font( attribute_dict['font_family']): logging.warning("{} not found, using default instead".format( attribute_dict['font_family'])) for font_attr in [ 'font_size', 'first_line_indent', 'left_indent', 'right_indent', 'leading', 'space_before', 'space_after', 'ending_cursor_pos', 'word_spacing' ]: if font_attr in attribute_dict: try: new_attr_dict[font_attr] = int(attribute_dict[font_attr]) except ValueError as e: logging.warning("{} warning: {}".format(font_attr, e)) # Get rid of xml font attribute, will use default later attribute_dict.pop(font_attr) for float_attr in ['border_width']: if float_attr in attribute_dict: try: new_attr_dict[float_attr] = float( attribute_dict[float_attr]) except ValueError as e: logging.warning("{} warning: {}".format(float_attr, e)) attribute_dict.pop(float_attr) for margin_attr in [ 'top_margin', 'bottom_margin', 'left_margin', 'right_margin' ]: if margin_attr in attribute_dict: try: new_attr_dict[margin_attr] = int( attribute_dict[margin_attr]) except ValueError as e: logging.warning("{} warning: {}".format(margin_attr, e)) attribute_dict.pop(margin_attr) return new_attr_dict
def merge_paragraph_styles(default_style: ParagraphStyle, bnml_style: Dict[str, Any]) -> ParagraphStyle: """Merges ReportLab ParagraphStyle attributes with Kassia bnml attributes and returns the new style :param default_style: The default ParagraphStyle (a ReportLab class). :param bnml_style: A dictionary of styles read a Kassia bnml file. The bnml_style needs to have ben already run through fill_dict_attributes(). :return new_style: A new ParagraphStyle of default_style with attributes updated by bnml_style """ new_style = deepcopy(default_style) if 'font_family' in bnml_style and font_reader.is_registered_font( bnml_style['font_family']): new_style.fontName = bnml_style['font_family'] if 'font_size' in bnml_style: new_style.fontSize = bnml_style['font_size'] if 'color' in bnml_style: new_style.textColor = bnml_style['color'] if 'bgcolor' in bnml_style: new_style.backColor = bnml_style['bgcolor'] if 'align' in bnml_style: new_style.alignment = bnml_style['align'] if 'first_line_indent' in bnml_style: new_style.firstLineIndent = bnml_style['first_line_indent'] if 'auto_leading' in bnml_style: new_style.autoLeading = bnml_style['auto_leading'] if 'leading' in bnml_style: new_style.leading = bnml_style['leading'] if 'left_indent' in bnml_style: new_style.leftIndent = bnml_style['left_indent'] if 'right_indent' in bnml_style: new_style.rightIndent = bnml_style['right_indent'] if 'space_before' in bnml_style: new_style.spaceBefore = bnml_style['space_before'] if 'space_after' in bnml_style: new_style.spaceAfter = bnml_style['space_after'] if 'word_spacing' in bnml_style: new_style.wordSpace = bnml_style['word_spacing'] if 'border_width' in bnml_style: new_style.borderWidth = bnml_style['border_width'] if 'border_color' in bnml_style: new_style.borderColor = bnml_style['border_color'] return new_style
def fill_attribute_dict(attribute_dict): """parse the color""" if 'color' in attribute_dict: if re.match("#[0-9a-fA-F]{6}", attribute_dict['color']): col = [z / 255. for z in hex_to_rgb(attribute_dict['color'])] attribute_dict['color'] = colors.Color(col[0], col[1], col[2], 1) else: attribute_dict.pop('color') """parse the font family""" if 'font_family' in attribute_dict: if not font_reader.is_registered_font( attribute_dict['font_family']): logging.warning("{} not found, using Helvetica instead".format( attribute_dict['font_family'])) # Helvetica is built into ReportLab, so we know it's safe attribute_dict['font_family'] = "Helvetica" """parse the font attributes""" for font_attr in [ 'font_size', 'first_line_indent', 'left_indent', 'right_indent' ]: if font_attr in attribute_dict: try: attribute_dict[font_attr] = int(attribute_dict[font_attr]) except ValueError as e: logging.warning("{} warning: {}".format(font_attr, e)) # Get rid of xml font attribute, will use default later attribute_dict.pop(font_attr) """parse the margins""" for margin_attr in [ 'top_margin', 'bottom_margin', 'left_margin', 'right_margin' ]: if margin_attr in attribute_dict: try: attribute_dict[margin_attr] = int( attribute_dict[margin_attr]) except ValueError as e: logging.warning("{} warning: {}".format(margin_attr, e)) # Get rid of xml margin attribute, will use default later attribute_dict.pop(margin_attr) return attribute_dict