Skip to content

feature: replace scalar placeholders in template #99

Open
@yanes32

Description

@yanes32

Hi Steve!

We are using your library to fill document templates. We put some tags in the template and then we try to replace those tags by code using your library, e.g. this tag: <COMPANY_NAME> should be replaced with the real company name. A function like this would be helpful: “def replace(search_text, replace_text)”. On Google we found some solutions and we implemented some methods. I would like to share all that code which is below, the only problem I guess is the text format loosing mainly on table cells. Could you include this feature in a future release?

Thanks for your time.

"""Added functions for search and replace"""

    def paragraph_search(self, text_value):
        result = False
        para_regex = re.compile(text_value)
        for paragraph in self.paragraphs:
            if paragraph.text:
                if para_regex.search(paragraph.text):
                    result = True
        return result

    def paragraph_replace(self, search, replace):
        searchre = re.compile(search)
        for paragraph in self.paragraphs:
            paragraph_text = paragraph.text
            if paragraph_text:
                if searchre.search(paragraph_text):
                    self.clear_paragraph(paragraph)
                    paragraph.add_run(re.sub(search, replace, paragraph_text))
        return paragraph

    def clear_paragraph(self, paragraph):
        p_element = paragraph._p
        p_child_elements = [elm for elm in p_element.iterchildren()]
        for child_element in p_child_elements:
            p_element.remove(child_element)

    def table_search(self, text_value):
        result = False
        tbl_regex = re.compile(text_value)
        for table in self.tables:
            for row in table.rows:
                for cell in row.cells:
                    for paragraph in cell.paragraphs:
                        if paragraph.text:
                            if tbl_regex.search(paragraph.text):
                                result = True
        return result

    def table_replace(self, search, replace):
        tbl_regex = re.compile(search)
        for table in self.tables:
            for row in table.rows:
                for cell in row.cells:
                    for paragraph in cell.paragraphs:
                        if paragraph.text:
                            if tbl_regex.search(paragraph.text):
                                paragraph_text = paragraph.text
                                self.clear_paragraph(paragraph)

paragraph.add_run(tbl_regex.sub(search, replace, paragraph_text))
        return paragraph

    def clear_table(self, table):
        return


    """Custom added functions for search and replace"""


    def tag_replace(self, table, search, replace):
        """
        Replace all occurences of tag within a table with a different string,
        return nothing
        """
        searchre = re.compile(search)
        for table in self.tables:
            for row in table.rows:
                for cell in row.cells:
                    for paragraph in cell.paragraphs:
                        if paragraph.text:
                            if searchre.search(paragraph.text):
                                paragraph_text = paragraph.text
                                self.clear_paragraph(paragraph)
                                paragraph.add_run(re.sub(search,
replace, paragraph_text))
        return

    def replace(self, document, search, replace):
        """
        Replace all occurences of string with a different string,
        return updated document
        """
        newdocument = document
        searchre = re.compile(search)
        for element in newdocument.paragraphs:
            if element._p.prefix == 'w':  # t (text) elements
                if element.text:
                    if searchre.search(element.text):
                        text = element.text
                        self.clear_paragraph(element)
                        element.add_run(re.sub(search, replace, text))
                        # element.text = re.sub(search, replace, element.text)
        return newdocument

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions