往docx写入表格信息为什么没有写入进去呢

代码

def new_template(sections, task_id):
    file_path = os.path.join(settings.UPLOAD_PATH, f"{task_id}.docx")

    # 设置 Aspose.Words 许可证
    lic = aw.License()
    lic_path = os.path.join(settings.BASE_PATH, "core/Aspose.Total.Product.Family.lic")
    lic.set_license(lic_path)

    # 创建一个新的文档
    doc = aw.Document()
    builder = aw.DocumentBuilder(doc)

    # 遍历节(section)
    for section in sections:
        if section["type"] == 'title':
            # 设置标题样式
            level=section.get('level',1)
            builder.paragraph_format.style_identifier = getattr(
              aw.StyleIdentifier, f"HEADING{level}"
            )
            # 添加标题内容
            title = section.get('contents', '')  # 获取标题内容
            builder.writeln(title)  # 写入标题到文档

        elif section["type"] == 'text':
            # 添加文本内容
            text_content = section.get('contents', '')  # 获取文本内容
            builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
            builder.writeln(text_content)  # 写入文本到文档

        elif section["type"] == 'table':
            # 添加表格内容
            table_data = section.get('contents', {})  # 获取表格内容
            table = builder.start_table()  # 开始一个新表格

            # 遍历表格行
            for row_data in table_data.get('content', []):
                table_row = builder.insert_cell()  # 插入新的表格行
                # 遍历表格单元格
                for cell_data in row_data.get('content', []):
                    cell_content = ''  # 单元格内容初始化为空字符串
                    if isinstance(cell_data, dict):  # 检查单元格内容是否为字典类型
                        paragraph_content = cell_data.get('content', [])
                        # 遍历段落内容
                        for paragraph in paragraph_content:
                            text = paragraph.get('text', '')  # 获取段落文本内容
                            cell_content += text  # 将段落内容添加到单元格内容中
                    else:
                        cell_content = cell_data  # 如果不是字典类型,则直接使用内容
                    colspan = cell_data.get('attrs', {}).get('colspan', 1)  # 获取列合并数
                    rowspan = cell_data.get('attrs', {}).get('rowspan', 1)  # 获取行合并数

                    # 合并单元格
                    if colspan > 1:
                        builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                    if rowspan > 1:
                        builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST  # 垂直合并第一个单元格

                    table_row.cell_format.wrap_text = True  # 设置单元格内容自动换行
                    table_row.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER  # 设置单元格内容垂直居中

                    builder.write(cell_content)  # 写入单元格内容

            builder.end_table()  # 结束表格

    # 保存文档
    doc.save(file_path)
    return file_path


new_template(sections, '11111hhhhh')

传入的信息

sections = [{
    "type": "title", "level": 1, "contents": "1、你好"
},
    {
        "type": "text", "contents": "哈哈哈哈哈"
    },
    {
        "type": "table", "contents": {
        "type": "table",
        "attrs": {
            "id": "ebf4362db15f18b8&&dbda60e3b9f8d503"
        },
        "content": [
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "机构\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "负责人\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 4,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "研究中心\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 2,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "广东省人民医院\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "主要研究者:吴一龙\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "主要研究者: 杨衿记\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "浙江大学附属第一医院\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,
                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "主要研究者:周建英\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "湖南省肿瘤医院\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "主要研究者:杨农\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "申办方\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "江苏万邦生化医药集团有限责任公司\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "项目负责人:周永春\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "合同研究组织\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "北京复星医药科技开发有限公司\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "项目经理:程艳秋\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "生物检测单位\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "广东省人民医院I期研究室\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "项目负责人:王曦培\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "数据管理单位\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "北京复星医药科技开发有限公司\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "数据管理经理:蔡红霞\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "统计单位\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "北京复星医药科技开发有限公司\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "生物统计经理:周艳玲\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "type": "tableRow",
                "content": [
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "中心实验室\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "昆皓睿诚医药研发(北京)有限公司\u0007"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "type": "tableCell",
                        "attrs": {
                            "colspan": 1,
                            "rowspan": 1,

                        },
                        "content": [
                            {
                                "type": "paragraph",
                                "content": [
                                    {
                                        "type": "text",
                                        "text": "项目经理:辜朝霞\u0007"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
    },
    {
        "type": "title", "leve": 1, "contents": "哈哈哈哈"
    },
]

目前存在2个问题,
1、标题不需要缩进,如果我插入2个标题,第二标题为什么缩进了
2、表格数据没有插入进去
以下是提供的代码,数据已经在上面上传了

def new_template(sections, task_id):
    file_path = os.path.join(settings.UPLOAD_PATH, f"{task_id}.docx")

    # 设置 Aspose.Words 许可证
    lic = aw.License()
    lic_path = os.path.join(settings.BASE_PATH, "core/Aspose.Total.Product.Family.lic")
    lic.set_license(lic_path)

    # 创建一个新的文档
    doc = aw.Document()
    builder = aw.DocumentBuilder(doc)

    # 遍历节(section)
    for section in sections:
        if section["type"] == 'title':
            # 设置标题样式
            level = section.get('level', 1)
            builder.paragraph_format.style_identifier = getattr(
                aw.StyleIdentifier, f"HEADING{level}"
            )
            # 添加标题内容
            new_run = builder.font
            # 设置西文和中文字体
            new_run.size = 12
            new_run.name = "Times New Roman"  # 设置西文是新罗马字体
            new_run.name_far_east = "宋体"
            # 添加标题内容
            title = section.get('contents', '')  # 获取标题内容
            builder.writeln(title)  # 写入标题到文档

        elif section["type"] == 'text':
            # 添加文本内容
            new_run = builder.font
            # 设置西文和中文字体
            new_run.name = "Times New Roman"  # 设置西文是新罗马字体
            new_run.name_far_east = "宋体"
            new_run.size = 12
            text_content = section.get('contents', '')  # 获取文本内容
            builder.paragraph_format.character_unit_first_line_indent = 2
            builder.paragraph_format.line_spacing = 18
            builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
            builder.paragraph_format.line_unit_after = 0
            builder.paragraph_format.space_after = 0
            builder.writeln(text_content)  # 写入文本到文档

        elif section["type"] == 'table':
            # 添加表格内容
            table_data = section.get('contents', {})  # 获取表格内容
            table = builder.start_table()  # 开始一个新表格

            # 遍历表格行
            for row_data in table_data.get('content', []):
                table_row = builder.insert_cell()  # 插入新的表格行
                # 遍历表格单元格
                for cell_data in row_data.get('content', []):
                    cell_content = ''  # 单元格内容初始化为空字符串
                    if isinstance(cell_data, dict):  # 检查单元格内容是否为字典类型
                        paragraph_content = cell_data.get('content', [])
                        # 遍历段落内容
                        for paragraph in paragraph_content:
                            text = paragraph.get('text', '')  # 获取段落文本内容
                            cell_content += text  # 将段落内容添加到单元格内容中
                    else:
                        cell_content = cell_data  # 如果不是字典类型,则直接使用内容
                    colspan = cell_data.get('attrs', {}).get('colspan', 1)  # 获取列合并数
                    rowspan = cell_data.get('attrs', {}).get('rowspan', 1)  # 获取行合并数

                    # 合并单元格
                    if colspan > 1:
                        builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                    if rowspan > 1:
                        builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST  # 垂直合并第一个单元格

                    table_row.cell_format.wrap_text = True  # 设置单元格内容自动换行
                    table_row.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER  # 设置单元格内容垂直居中

                    builder.write(cell_content)  # 写入单元格内容

            builder.end_table()  # 结束表格

    # 保存文档
    doc.save(file_path)
    return file_path

@hhh1111 该问题与Aspose.Words无关。 问题是您的代码没有从数据源获取数据。 因此,在向 Aspose.Words 报告问题之前,您应该首先确保正确读取数据源中的数据。 请尝试像这样修改代码:

# 创建一个新的文档
doc = aw.Document()
builder = aw.DocumentBuilder(doc)

# 遍历节(section)
for section in sections:
    if section["type"] == 'title':
        # 设置标题样式
        level=section.get('level',1)
        builder.paragraph_format.style_identifier = getattr(
            aw.StyleIdentifier, f"HEADING{level}"
        )
        # 添加标题内容
        title = section.get('contents', '')  # 获取标题内容
        builder.writeln(title)  # 写入标题到文档

    elif section["type"] == 'text':
        # 添加文本内容
        text_content = section.get('contents', '')  # 获取文本内容
        builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
        builder.writeln(text_content)  # 写入文本到文档

    elif section["type"] == 'table':
        # 添加表格内容
        table_data = section.get('contents', {})  # 获取表格内容
        table = builder.start_table()  # 开始一个新表格

        # 遍历表格行
        for row_data in table_data.get('content', []):
            # 遍历表格单元格
            for cell_data in row_data.get('content', []):
                table_row = builder.insert_cell()  # 插入新的表格行
                cell_content = ''  # 单元格内容初始化为空字符串
                if isinstance(cell_data, dict):  # 检查单元格内容是否为字典类型
                    for paragraph_content in cell_data.get('content', []):
                        for paragraph in paragraph_content.get('content', []):
                             text = paragraph["text"]  # 获取段落文本内容
                             cell_content += text  # 将段落内容添加到单元格内容中
                else:
                    cell_content = cell_data  # 如果不是字典类型,则直接使用内容
                colspan = cell_data.get('attrs', {}).get('colspan', 1)  # 获取列合并数
                rowspan = cell_data.get('attrs', {}).get('rowspan', 1)  # 获取行合并数

                # 合并单元格
                if colspan > 1:
                    builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                if rowspan > 1:
                    builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST  # 垂直合并第一个单元格

                table_row.cell_format.wrap_text = True  # 设置单元格内容自动换行
                table_row.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER  # 设置单元格内容垂直居中

                builder.write(cell_content)  # 写入单元格内容
            builder.end_row()
        builder.end_table()  # 结束表格

# 保存文档
doc.save("C:\\Temp\\out.docx")

可以帮我看下 我的单元格合并规则怎么没有写入进去呀
还有一个问题 如果我设置type 类型为text段落缩进 ,为什么标题表格信息也都缩进了,怎么设置,只让段落进行首行缩进呢

@hhh1111 在代码中,您仅设置第一个CellMerge.FIRST,但不要在任何地方指定CellMerge.PREVIOUS。 请参阅我们的文档以了解如何使用合并单元格:
https://docs.aspose.com/words/python-net/working-with-merged-cells/

请告诉我需要怎么进行修改呢????

@hhh1111 以下是更新后的代码:

def aw_read_table(self, table, tables):
    table.convert_to_horizontally_merged_cells()

    table_data = []
    for row in table.rows:
        content = {
            "type": "tableRow",
            "content": []
        }
        row_index = table.index_of(row)
        cell = row.as_row().first_cell
        row_span = 1
        col_span = 1
        current_cell = cell
        cell_index = 0
        cell_text = ""
        while current_cell is not None:
            cell_index = current_cell.parent_row.index_of(current_cell)
            if current_cell.cell_format.vertical_merge == aw.tables.CellMerge.FIRST and current_cell.cell_format.horizontal_merge == aw.tables.CellMerge.FIRST:
                cell_text = current_cell.get_text()
                current_cell = current_cell.next_cell
                for i in range(row_index, table.rows.count):
                    if table.rows[i].cells[cell_index].cell_format.vertical_merge == aw.tables.CellMerge.PREVIOUS:
                        row_span += 1
                while current_cell is not None and current_cell.cell_format.horizontal_merge == aw.tables.CellMerge.PREVIOUS:
                    col_span = col_span + 1
                    current_cell = current_cell.next_cell
            elif current_cell.cell_format.horizontal_merge == aw.tables.CellMerge.FIRST:
                cell_text = current_cell.get_text()
                current_cell = current_cell.next_cell
                while current_cell is not None and current_cell.cell_format.horizontal_merge == aw.tables.CellMerge.PREVIOUS:
                    col_span = col_span + 1
                    current_cell = current_cell.next_cell
            elif current_cell.cell_format.vertical_merge == aw.tables.CellMerge.FIRST:
                cell_text = current_cell.get_text()
                cell_index = current_cell.parent_row.index_of(current_cell)
                for i in range(row_index, table.rows.count):
                    if table.rows[i].cells[cell_index].cell_format.vertical_merge == aw.tables.CellMerge.PREVIOUS:
                        row_span += 1
                current_cell = current_cell.next_cell
            else:
                # move to the next cell in the row.
                cell_text = current_cell.get_text()
                current_cell = current_cell.next_cell

            cell_content = {
                "type": "tableCell",
                "attrs": {
                    "colspan": col_span,
                    "rowspan": row_span,
                    "colwidth": None
                },
                "content": []
            }

            # Add paragraph content to the cell
            paragraph = {
                "type": "paragraph",
                "content": [
                    {
                        "type": "text",
                        "text": cell_text,
                    }
                ]
            }
            cell_content["content"].append(paragraph)
            content["content"].append(cell_content)

            col_span = 1
            row_span = 1

        table_data.append(content)

    return table_data
doc = aw.Document()
builder = aw.DocumentBuilder(doc)

table = builder.start_table()  # 开始一个新表格
for i in range(len(table_data)):
    cell_data_index = 0
    for row_data in table_data[i].get('content', []):
        # 遍历表格单元格
        for cell_data in row_data.get('content', []):
            new_cell = builder.insert_cell()  # 插入新的表格行
            builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE

            row_index = table.index_of(new_cell)
            cell_index = new_cell.parent_row.index_of(new_cell)

            previous_row = table.rows[row_index - 1]
            if previous_row is not None:
                previous_cell = previous_row.cells[cell_index]
                if previous_cell is not None and \
                        (previous_cell.cell_format.vertical_merge == aw.tables.CellMerge.FIRST or
                         previous_cell.cell_format.vertical_merge == aw.tables.CellMerge.PREVIOUS):
                    for y in range(i, -1, -1):
                        curr_row_span = table_data[y].get('content', [])[cell_data_index].get('attrs', {}).get(
                            'rowspan', 1)
                        if curr_row_span > 1:
                            break
                    if curr_row_span >= table.rows.count:
                        builder.cell_format.vertical_merge = aw.tables.CellMerge.PREVIOUS
                    else:
                        builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE

            builder.cell_format.horizontal_merge = aw.tables.CellMerge.NONE
            cell_content = ''  # 单元格内容初始化为空字符串
            if isinstance(cell_data, dict):  # 检查单元格内容是否为字典类型
                for paragraph_content in cell_data.get('content', []):
                    text = paragraph_content["text"]  # 获取段落文本内容
                    cell_content += text  # 将段落内容添加到单元格内容中
            else:
                cell_content = cell_data  # 如果不是字典类型,则直接使用内容

            new_cell.cell_format.wrap_text = True  # 设置单元格内容自动换行
            new_cell.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER  # 设置单元格内容垂直居中

            builder.write(cell_content)  # 写入单元格内容

            colspan = row_data.get('attrs', {}).get('colspan', 1)  # 获取列合并数
            rowspan = row_data.get('attrs', {}).get('rowspan', 1)  # 获取行合并数

            if colspan > 1 and rowspan > 1:
                builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST
                builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                for i in range(1, colspan):
                    builder.insert_cell()
                    builder.cell_format.horizontal_merge = aw.tables.CellMerge.PREVIOUS
            elif colspan > 1:
                builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                for i in range(1, colspan):
                    builder.insert_cell()
                    builder.cell_format.horizontal_merge = aw.tables.CellMerge.PREVIOUS
            elif rowspan > 1:
                builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST  # 垂直合并第一个单元格

            cell_data_index += 1

    builder.end_row()
builder.end_table()

builder.document.save("output.docx")

通过这段代码,我创建了以下表格:
output.docx (12.1 KB)

以下是我为上表提供的数据:

row_index = 0	 colspan = 1	 rowspan = 1
row_index = 0	 colspan = 2	 rowspan = 3
row_index = 0	 colspan = 1	 rowspan = 3
row_index = 1	 colspan = 1	 rowspan = 1
row_index = 1	 colspan = 2	 rowspan = 1
row_index = 1	 colspan = 1	 rowspan = 1
row_index = 2	 colspan = 1	 rowspan = 1
row_index = 2	 colspan = 2	 rowspan = 1
row_index = 2	 colspan = 1	 rowspan = 1
row_index = 3	 colspan = 4	 rowspan = 1
row_index = 4	 colspan = 4	 rowspan = 1
row_index = 5	 colspan = 4	 rowspan = 1
row_index = 6	 colspan = 4	 rowspan = 1
row_index = 7	 colspan = 2	 rowspan = 1
row_index = 7	 colspan = 2	 rowspan = 1
row_index = 8	 colspan = 4	 rowspan = 1
row_index = 9	 colspan = 4	 rowspan = 1
row_index = 10	 colspan = 4	 rowspan = 1
row_index = 11	 colspan = 4	 rowspan = 1
row_index = 12	 colspan = 4	 rowspan = 1
row_index = 13	 colspan = 4	 rowspan = 1

如何做到这一点并没有通用的方法,也许还需要对某些表格进行一些改进。因此,这只是一个开发过程,而不是一个示例,你需要根据自己的目的进行改进。

好的 感谢 如果我只给段落内容设置首行缩进 为什么段落下面的格式都变成首行缩进了

def new_template(sections, task_id):
    file_path = os.path.join(settings.UPLOAD_PATH, f"{task_id}.docx")

    # 设置 Aspose.Words 许可证
    lic = aw.License()
    lic_path = os.path.join(settings.BASE_PATH, "core/Aspose.Total.Product.Family.lic")
    lic.set_license(lic_path)

    # 创建一个新的文档
    doc = aw.Document()
    builder = aw.DocumentBuilder(doc)

    # 遍历节(section)
    for section in sections:
        if section["type"] == 'title':
            # 设置标题样式
            level = section.get('level', 1)
            builder.paragraph_format.style_identifier = getattr(
                aw.StyleIdentifier, f"HEADING{level}"
            )
            # 添加标题内容
            new_run = builder.font
            # 设置西文和中文字体
            new_run.size = 12
            new_run.name = "Times New Roman"  # 设置西文是新罗马字体
            new_run.name_far_east = "宋体"
            # 添加标题内容
            title = section.get('contents', '')  # 获取标题内容
            builder.writeln(title)  # 写入标题到文档

        elif section["type"] == 'text':
            # 添加文本内容
            new_run = builder.font
            # 设置西文和中文字体
            new_run.name = "Times New Roman"  # 设置西文是新罗马字体
            new_run.name_far_east = "宋体"
            new_run.size = 12
            text_content = section.get('contents', '')  # 获取文本内容
            builder.paragraph_format.character_unit_first_line_indent = 2
            builder.paragraph_format.line_spacing = 18
            builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
            builder.paragraph_format.line_unit_after = 0
            builder.paragraph_format.space_after = 0
            builder.writeln(text_content)  # 写入文本到文档

        elif section["type"] == 'table':
            # 添加表格内容
            table_data = section.get('contents', {})  # 获取表格内容
            table = builder.start_table()  # 开始一个新表格
            # 遍历表格行
            for row_data in table_data.get('content', []):
                # 遍历表格单元格
                for cell_data in row_data.get('content', []):
                    table_row = builder.insert_cell()  # 插入新的表格行
                    cell_content = ''  # 单元格内容初始化为空字符串
                    if isinstance(cell_data, dict):  # 检查单元格内容是否为字典类型
                        for paragraph_content in cell_data.get('content', []):
                            for paragraph in paragraph_content.get('content', []):
                                text = paragraph["text"]  # 获取段落文本内容
                                cell_content += text  # 将段落内容添加到单元格内容中
                    else:
                        cell_content = cell_data  # 如果不是字典类型,则直接使用内容
                    colspan = cell_data.get('attrs', {}).get('colspan', 1)  # 获取列合并数
                    rowspan = cell_data.get('attrs', {}).get('rowspan', 1)  # 获取行合并数
                    # 合并单元格
                    if colspan > 1:
                        builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST  # 水平合并第一个单元格
                    if rowspan > 1:
                        builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST  # 垂直合并第一个单元格

                    table_row.cell_format.wrap_text = True  # 设置单元格内容自动换行
                    table_row.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER  # 设置单元格内容垂直居中
                    builder.write(cell_content)  # 写入单元格内容
                builder.end_row()

            builder.end_table()  # 结束表格

    # 保存文档
    doc.save(file_path)
    return file_path

@hhh1111 这是因为您在 DocumentBuilder 中设置了段落格式并继续使用它。您需要在其他地方重新设置这种格式。

for section in sections:
    builder.paragraph_format.clear_formatting()