0
votes

I have some functions that successfully replace specific strings from a .docx file (in both paragraphs and tables) with new strings.

Everything with the replacing part of the function works great. However, the "spacing after paragraph" values change when I replace the strings in the paragraph. I want the spacing to stay the same, or I want to be able to change the spacing back to its original format after the replacement occurs.

I am unfamiliar with docx, so maybe its just a simple solution that I missed?

A sample of the word .docx file can be downloaded here: https://filebin.net/9t5v96tb5y7z0e60

The working paragraph and table string replacement code:

"""
Script to replace specific texts within word doc templates
"""

import re, docx
from docx import Document


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 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):
                clear_paragraph(self, paragraph)
                paragraph.add_run(re.sub(search, replace, paragraph_text))
    return paragraph


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


regex1 = ["<<authors>>", "<<author>>", "<<id>>",        \
            "<<title>>", "<<date>>", "<<discipline>>",  \
            "<<countries>>"]

author = "Robert"
authors = "Robert, John; Bob, Billy; Duck, Donald"
ms_id = "2020-34-2321"
title = "blah blah blah and one more blah"
date = "31-03-2020"
discipline = "BE"
countries = "United States, Japan, China, South Africa"

replace1 = [authors, author, ms_id, title, date, discipline, countries]

filename = "Sample Template.docx"
doc = Document(filename)

for x in range(len(regex1)):
    paragraph_replace(doc, regex1[x], replace1[x])
    table_replace(doc, regex1[x], replace1[x])

doc.save(author + '_updated.docx')
1

1 Answers

0
votes

After reading more of Docx documentation and some testing, the solution to this problem was easy.

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 paragraph_replace(self, search, replace, x):
    searchre = re.compile(search)
    for paragraph in self.paragraphs:
        paragraph_text = paragraph.text
        if paragraph_text:
            if searchre.search(paragraph_text):
                clear_paragraph(self, paragraph)
                para = paragraph.add_run(re.sub(search, replace, paragraph_text))
                para.font.size = Pt(10)
                paragraph.paragraph_format.space_after=Pt(0)
                if x is 2:
                    para.bold = True
                else:
                    para.bold = False
                paragraph.paragraph_format.line_spacing = 1.0
    return paragraph


def table_replace(self, text_value, replace):
    result = False
    tbl_regex = re.compile(text_value)
    for table in self.tables:
        for row in table.rows:
            for cell in row.cells:
                paragraphs = cell.paragraphs
                for paragraph in paragraphs:
                    for run in paragraph.runs:
                        font = run.font
                        font.size=Pt(10)
                if cell.text:
                    if tbl_regex.search(cell.text):
                        cell.text = replace
                        result = True
    return result

I added the x argument in the paragraph_replace function because I wanted the first line of my document to be bold. All my issues are now resolved with these simple additions to the code.