How to use the docxcompose.properties.FieldBase function in docxcompose

To help you get started, we’ve selected a few docxcompose examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 4teamwork / docxcompose / docxcompose / properties.py View on Github external
""" Removes the field from the document, replacing it with
        its value.
        """
        raise NotImplementedError()

    def _get_fieldname_string(self):
        raise NotImplementedError()

    def _parse_fieldname(self):
        match = self.fieldname_search_expr.search(self._get_fieldname_string())
        if match is None:
            return None
        return match.groups()[0]


class SimpleField(FieldBase):
    """ Represents a simple field, i.e.  node in the
    document.xml, its body containing the value of the field.
    self.node here is the  node.
    """

    attr_name = "{{{}}}instr".format(NS["w"])

    def _get_fieldname_string(self):
        return self.node.attrib[self.attr_name]

    def update(self, value):
        text = xpath(self.node, './/w:t')
        if text:
            text[0].text = self._format_value(value)

    def replace_field_with_value(self):
github 4teamwork / docxcompose / docxcompose / properties.py View on Github external
text[0].text = self._format_value(value)

    def replace_field_with_value(self):
        parent = self.node.getparent()
        index = list(parent).index(self.node)
        w_r = deepcopy(self.node[0])
        parent.remove(self.node)
        parent.insert(index, w_r)


class InvalidComplexField(Exception):
    """This exception is raised when a complex field cannot
    be handled correctly."""


class ComplexField(FieldBase):
    """ Represents a complex field, i.e. a several  nodes delimited by runs
    containing  and .
    In these fields, the actual value is stored in  nodes that come after a
     node.
    """

    XPATH_PRECEDING_BEGINS = "./preceding-sibling::w:r/w:fldChar[@w:fldCharType=\"begin\"]/.."
    XPATH_FOLLOWING_ENDS = "./following-sibling::w:r/w:fldChar[@w:fldCharType=\"end\"]/.."
    XPATH_FOLLOWING_SEPARATES = "./following-sibling::w:r/w:fldChar[@w:fldCharType=\"separate\"]/.."

    def __init__(self, field_node):
        super(ComplexField, self).__init__(field_node)
        # run and paragraph containing the field
        self.w_r = self.node.getparent()
        self.w_p = self.w_r.getparent()