Replace text in docx file

Hạ Vũ 0 Reputation points
2024-03-09T09:52:44.5666667+00:00

Hi, I have this Python code to replace text with context. It seems like the code worked, but not all the text in the Word file has been replaced. I have checked the formatting, but there is no difference between the two paragraphs when the code is not running on the entire document.

#! env/Scripts/python
import os
import PySimpleGUI as sg
from docx import Document
from docx.shared import RGBColor

def render_files_in_folder(folder_path, contexts):
    try:
        # Get the list of files in the folder
        files = os.listdir(folder_path)
        # Loop through each file and render
        for file_name in files:
            # Check if it's a .docx file
            if file_name.endswith('.docx'):
                file_path = os.path.join(folder_path, file_name)
                print(f"Đang xử lý: {file_path}")
                doc = Document(file_path)
                for context in contexts:
                    replace_text_in_docx(doc, context)
                doc.save(file_path)  # Save the file after rendering
    except Exception as e:
        print(f"An error occurred: {e}")

def replace_text_in_docx(doc, context):
    color = RGBColor(255, 0, 0)
    for paragraph in doc.paragraphs:
        for key, value in context.items():
            if key in paragraph.text:
                inline = paragraph.runs
                for i in range(len(inline)):
                    if key in inline[i].text:
                        # Replace the text
                        inline[i].text = inline[i].text.replace(key, value)
                        # Apply formatting: red color and bold
                        inline[i].font.color.rgb = color  # Red color
                        inline[i].font.bold = True
        if paragraph.text.startswith("[") and paragraph.text.endswith("]"):
            print("Chỗ này sót nè má:", paragraph.text)

    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                for paragraph in cell.paragraphs:
                    for key, value in context.items():
                        if key in paragraph.text:
                            inline = paragraph.runs
                            for i in range(len(inline)):
                                if key in inline[i].text:
                                    # Replace the text
                                    inline[i].text = inline[i].text.replace(key, value)
                                    # Apply formatting: red color and bold
                                    inline[i].font.color.rgb = color  # Red color
                                    inline[i].font.bold = True
                    if paragraph.text.startswith("[") and paragraph.text.endswith("]"):
                        print("Chỗ này sót nè má:", paragraph.text)    

    for section in doc.sections:
        header = section.header
        footer = section.footer
        replace_content_in_header_footer(header, context)
        replace_content_in_header_footer(footer, context)

def replace_content_in_header_footer(part, context):
    for paragraph in part.paragraphs:
        for key, value in context.items():
            if key in paragraph.text:
                inline = paragraph.runs
                for i in range(len(inline)):
                    if key in inline[i].text:
                        # Replace the text
                        inline[i].text = inline[i].text.replace(key, value)
                        # Apply formatting: red color and bold
                        inline[i].font.color.rgb = (255, 0, 0)  # Red color
                        inline[i].font.bold = True
        if paragraph.text.startswith("[") and paragraph.text.endswith("]"):
            print("Chỗ này sót nè má:", paragraph.text)

# Input layout
col_1 = [
    [sg.Text("Đường dẫn thư mục:", size=(15, 1))],
]

col_2 = [
    [sg.Text("Giá trị cần thay thế:", size=(15, 1))],
]

col_3 = [
    [sg.Text("Giá trị thay thế:", size=(15, 1))],
]

col_4 = [
    [sg.InputText(key="folder_path"), sg.FolderBrowse()],
]

col_5 = [
    [sg.InputText(key="key")],
]

col_6 = [
    [sg.InputText(key="value")],
]

layout = [
    [
        [col_1, col_4],
        [col_2, col_5],
        [col_3, col_6]
    ],
    [sg.Button("Thêm giá trị"), sg.Button("Bắt đầu")],
    [sg.Output(size=(60, 20), key = '_output_')],
    [sg.Button("Thoát")]
]

window = sg.Window("Thay thế chữ trong docx file <3", layout)

contexts = []

# Main loop for the interface
while True:
    event, values = window.Read()

    if event == sg.WIN_CLOSED or event == "Exit":
        break
    elif event == "Thêm giá trị":
        key = values["key"]
        value = values["value"]
        if key and value:
            context = {key: value}
            contexts.append(context)
            print(f"Đã thêm giá trị: {context}")
        else:
            print("Hãy nhập đủ các trường giá trị!")
    elif event == "Bắt đầu":
        window.FindElement('_output_').Update('')
        folder_path = values["folder_path"]
        pre_folder_path = folder_path
        if pre_folder_path != folder_path:
            contexts.clear()
            pre_folder_path = folder_path
        if folder_path:
            # Create a new dictionary to store all contexts
            combined_context = {}
            for context in contexts:
                # Merge key-value pairs of each context into the main dictionary
                combined_context.update(context)
            try:
                render_files_in_folder(folder_path, [combined_context])
                print("Hoàn tất!")
            except Exception as e:
                print(f"An error occurred: {e}")
        else:
            print("Hãy nhập đường dẫn thư mục!")
window.close()

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
687 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,570 questions
0 comments No comments
{count} votes