from spire.doc import *


# 将文本写入文件
def WriteAllText(fname: str, text: str):
    with open(fname, "w", encoding="utf-8") as fp:
        fp.write(text)


inputFile = "/home/ccran/lufa-contract/demo/修订测试.docx"
outputFile1 = "/home/ccran/lufa-contract/demo/新增修订.txt"
outputFile2 = "/home/ccran/lufa-contract/demo/删除修订.txt"

# 创建Document类的对象
document = Document()

# 加载Word文档
document.LoadFromFile(inputFile)
print(document.GetText())

# 初始化列表来收集文本片段
insert_revisions = []
delete_revisions = []

# 遍历文档所有章节
for k in range(document.Sections.Count):
    sec = document.Sections.get_Item(k)

    # 遍历章节中的正文元素
    for m in range(sec.Body.ChildObjects.Count):
        # Check if the item is a Paragraph
        docItem = sec.Body.ChildObjects.get_Item(m)
        if isinstance(docItem, Paragraph):
            para = docItem

            # 判断段落是否为插入修订
            if para.IsInsertRevision:
                # 获取修订的类型，作者及其关联的内容
                insRevison = para.InsertRevision
                insType = insRevison.Type
                insAuthor = insRevison.Author
                insert_revisions.append(f"Revision Type: {insType.name}\n")
                insert_revisions.append(f"Revision Author: {insAuthor}\n")
                insert_revisions.append(f"Insertion Text: {para.Text}\n\n")
            # 判断段落是否为删除修订
            elif para.IsDeleteRevision:
                # 获取修订的类型，作者及其关联的内容
                delRevison = para.DeleteRevision
                delType = delRevison.Type
                delAuthor = delRevison.Author
                delete_revisions.append(f"Revision Type:: {delType.name}\n")
                delete_revisions.append(f"Revision Author: {delAuthor}\n")
                delete_revisions.append(f"Deletion Text: {para.Text}\n\n")
            # 如果段落没有修订，则遍历段落中的元素
            else:
                for j in range(para.ChildObjects.Count):
                    obj = para.ChildObjects.get_Item(j)
                    if isinstance(obj, TextRange):
                        textRange = obj

                        # 判断textrange是否为插入修订
                        if textRange.IsInsertRevision:
                            # 获取修订的类型，作者及其关联的内容
                            insRevison = textRange.InsertRevision
                            insType = insRevison.Type
                            insAuthor = insRevison.Author
                            insert_revisions.append(f"Revision Type: {insType.name}\n")
                            insert_revisions.append(f"Revision Author: {insAuthor}\n")
                            insert_revisions.append(
                                f"Insertion Text: {textRange.Text}\n"
                            )
                        # 判断textrange是否为删除修订
                        elif textRange.IsDeleteRevision:
                            # 获取修订的类型，作者及其关联的内容
                            delRevison = textRange.DeleteRevision
                            delType = delRevison.Type
                            delAuthor = delRevison.Author
                            delete_revisions.append(f"Revision Type: {delType.name}\n")
                            delete_revisions.append(f"Revision Author: {delAuthor}\n")
                            delete_revisions.append(
                                f"Deletion Text: {textRange.Text}\n"
                            )

# 写出修订信息到文件
WriteAllText(outputFile1, "".join(insert_revisions))
WriteAllText(outputFile2, "".join(delete_revisions))

# 释放资源
document.Dispose()
