from docx import Document
from docx.shared import RGBColor

def find_differences(file1_path, file2_path):
    differences = []

    doc1 = Document(file1_path)
    doc2 = Document(file2_path)

    for para_num, (para1, para2) in enumerate(zip(doc1.paragraphs, doc2.paragraphs), start=1):
        if para1.text != para2.text:
            differences.append((para_num, para1.text, para2.text))

    return differences

def highlight_differences(docx_path, differences):
    doc = Document(docx_path)

    for line_num, line1, line2 in differences:
        para = doc.paragraphs[line_num - 1]

        # Replace line1 with line2 in the paragraph text
        para.clear()  # Clear existing runs
        para.add_run(line2)  # Add the corrected text

        # Highlight the entire line2 (new text)
        for run in para.runs:
            highlight_run(run, RGBColor(255, 255, 0))  # Yellow highlight for new text

    output_docx = docx_path.replace('.docx', '_modificato.docx')
    doc.save(output_docx)
    print(f"File {output_docx} saved with differences highlighted.")

def highlight_run(run, color):
    run.font.highlight_color = color  # Set highlight color

if __name__ == "__main__":
    file1_path = 'file1.docx'  # Percorso del primo documento DOCX
    file2_path = 'file2.docx'  # Percorso del secondo documento DOCX

    differences = find_differences(file1_path, file2_path)
    highlight_differences(file2_path, differences)

    print(f"Processo completato. File DOCX con le differenze evidenziate: file2_modificato.docx")
