Skip to content

How can I ensure that the table content remains the same when replacing the table content? #1512

@zhmingzhaung

Description

@zhmingzhaung
from docx import Document

def read_doc(document, old, new):
    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            if old in run.text:
                run.text = run.text.replace(old, new)
    
    for table in document.tables:
        for row in table.rows:
            for cell in row.cells:
                for paragraph in cell.paragraphs:
                    for run in paragraph.runs:
                        if old in run.text:
                            run.text = run.text.replace(old, new)

document = Document(r'data/test.docx')
read_doc(document, 'TOTTRADE', '测试输出')
document.save(r'data/test_out.docx')

I can replace the content of the table while maintaining its format unchanged using the aforementioned code. However, when I use the following code for processing, it changes the table format. Why is that?

from decimal import Decimal
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_LINE_SPACING
import re

def format_dict(d):
    for key, value in d.items():
        if isinstance(value, dict):
            d[key] = format_dict(value)
        elif isinstance(value, float):
            d[key] = float(Decimal(str(value)).quantize(Decimal('0.0')))
    return d

def run(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12, df13, month, this_year=2025, last_year=2024):
    data_sources = [
        (get_report_data, [df1, month, this_year, last_year]),
        (get_report_data0, [df2]),
        (get_report_data1, [df3]),
        (get_report_data2, [df4, df5, df6]),
        (get_report_data3, [df7, df8, df9]),
        (get_report_data4, [df10, df11, df12, df13])
    ]
    rs = {}
    for func, args in data_sources:
        rs.update(func(*args)) 
    
    combined_data = format_dict(rs)
    
    for key, value in combined_data.items():
        if isinstance(value, pd.Series):
            if len(value) == 1:
                combined_data[key] = value.item()  
            else:
                combined_data[key] = value.sum() 

    return combined_data

def replace_text_in_docx(file_name, combined_data, year, month):
    doc = Document(file_name)
    sorted_data = sorted(combined_data.items(), key=lambda x: len(x[0]), reverse=True)

    for old_text, new_text in sorted_data:
        for p in doc.paragraphs:
            for run in p.runs:
                if old_text in run.text:
                    run.text = run.text.replace(str(old_text), str(new_text))

        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    if old_text in cell.text:
                        for paragraph in cell.paragraphs:
                            for run in paragraph.runs:
                                run.text = run.text.replace(old_text, str(new_text))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions