插入docx

[
    {
        "content": "这是一个普通的段落,用于展示一般的文本内容,提供信息或描述情境。",
        "type": "普通段落"
    },
    {
        "content": "这是一个加粗的段落,强调重点内容或重要提示,吸引读者注意。",
        "type": "加粗段落"
    },
    {
        "content": "<table border=\"1\"><tr><th>姓名</th><th>年龄</th><th>城市</th></tr><tr><td>张三</td><td>28</td><td>北京</td></tr><tr><td>李四</td><td>32</td><td>上海</td></tr></table>",
        "type": "表格"
    }
]

数据是这样 有很多个数据 我希望普通段落和加粗段落是12 号字体
段前 0 pt,段后 6 pt,
表格内容是10号字体 表头要加醋。
段落内容单行间距;首行 2 ch。排除加粗段落。

@Tiaohh

要在Aspose.Words中插入和格式化段落及表格,您可以使用以下代码示例来满足您的需求。根据您的要求,普通段落和加粗段落的字体大小设置为12号,段前间距为0 pt,段后间距为6 pt;表格内容的字体大小设置为10号,表头加粗,段落内容为单行间距,首行缩进2个字符,且加粗段落不受首行缩进影响。

以下是实现该功能的代码示例:

import com.aspose.words.*;

public class DocumentExample {
    public static void main(String[] args) throws Exception {
        // 创建新文档
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // 添加普通段落
        builder.Font.Size = 12;
        builder.ParagraphFormat.SpaceBefore = 0;
        builder.ParagraphFormat.SpaceAfter = 6;
        builder.ParagraphFormat.FirstLineIndent = 2; // 首行缩进
        builder.Writeln("这是一个普通的段落,用于展示一般的文本内容,提供信息或描述情境。");

        // 添加加粗段落
        builder.Font.Bold = true;
        builder.Writeln("这是一个加粗的段落,强调重点内容或重要提示,吸引读者注意。");
        builder.Font.Bold = false; // 重置加粗状态

        // 添加表格
        Table table = builder.StartTable();
        builder.CellFormat.Width = 100; // 设置单元格宽度
        builder.Font.Size = 10; // 表格内容字体大小

        // 添加表头
        builder.InsertCell();
        builder.Font.Bold = true; // 表头加粗
        builder.Write("姓名");
        builder.InsertCell();
        builder.Write("年龄");
        builder.InsertCell();
        builder.Write("城市");
        builder.EndRow();

        // 添加表格内容
        builder.Font.Bold = false; // 重置加粗状态
        builder.InsertCell();
        builder.Write("张三");
        builder.InsertCell();
        builder.Write("28");
        builder.InsertCell();
        builder.Write("北京");
        builder.EndRow();

        builder.InsertCell();
        builder.Write("李四");
        builder.InsertCell();
        builder.Write("32");
        builder.InsertCell();
        builder.Write("上海");
        builder.EndRow();

        builder.EndTable();

        // 保存文档
        doc.Save("FormattedDocument.docx");
    }
}

在这个示例中,我们创建了一个新的Word文档,添加了普通段落和加粗段落,并设置了相应的格式。表格的内容和表头也按照您的要求进行了格式化。您可以根据需要进一步调整代码以满足特定的格式要求。

希望这个示例能帮助您实现所需的文档格式化!

这样不对 不对 ??????

表格内容是html格式的 怎么处理 ???? 完整的处理流程

需要按照顺序插入信息,设置对应的格式

@Tiaohh 您可以使用这样的代码来获得预期的输出结果:

json_data = [
    {
        "content": "这是一个普通的段落,用于展示一般的文本内容,提供信息或描述情境。",
        "type": "普通段落"
    },
    {
        "content": "这是一个加粗的段落,强调重点内容或重要提示,吸引读者注意。",
        "type": "加粗段落"
    },
    {
        "content": "<table border=\"1\"><tr><th>姓名</th><th>年龄</th><th>城市</th></tr><tr><td>张三</td><td>28</td><td>北京</td></tr><tr><td>李四</td><td>32</td><td>上海</td></tr></table>",
        "type": "表格"
    }
]

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

for item in json_data:
    content = item["content"]
    item_type = item["type"]

    # 创建段落并应用常用格式
    builder.insert_paragraph()
    builder.paragraph_format.space_before = 0
    builder.paragraph_format.space_after = 6
    builder.font.size = 12

    if item_type == "普通段落":
        # 应用一般段落格式
        builder.paragraph_format.first_line_indent = 24  # 2 Chinese characters (approx 24 points)
        builder.paragraph_format.line_spacing = 12  # Single line spacing
        builder.write(content)

    elif item_type == "加粗段落":
        builder.paragraph_format.first_line_indent = 0
        # 应用粗体格式
        builder.font.bold = True
        builder.write(content)
        builder.font.bold = False

    elif item_type == "表格":
        # 插入 HTML 表格
        builder.insert_html(content)

        # 格式化表头(如需要)
        tables = doc.get_child_nodes(aw.NodeType.TABLE, True)
        if tables.count > 0:
            table = tables[tables.count - 1].as_table()
            table.style_identifier = aw.StyleIdentifier.TABLE_GRID
            for cell in table.first_row.cells:
                cell = cell.as_cell()
                for para in cell.paragraphs:
                    para = para.as_paragraph()
                    for run in para.runs:
                        run = run.as_run()
                        run.font.size = 10

    builder.writeln()  # 在每个项目后添加换行符

doc.save(ARTIFACTS_DIR + "output.docx")

如果我想设置这个样式要怎么设置呢

import uuid
from typing import List, Optional

import aspose.words as aw
from bs4 import BeautifulSoup

lic = aw.License()
lic_path = "Aspose.Total.Product.Family.lic"
lic.set_license(lic_path)
json_data = [

{
    "content": "患者编号:0101027",
    "type": "加粗段落"
},
    {
        "content": "这是一个普通的段落,用于展示一般的文本内容,提供信息或描述情境。",
        "type": "普通段落"
    },
    {
        "content": "这是一个加粗的段落,强调重点内容或重要提示,吸引读者注意。",
        "type": "加粗段落"
    },
    {
        "content": "<table border=\"1\"><tr><th>姓名</th><th>年龄</th><th>城市</th></tr><tr><td>张三</td><td>28</td><td>北京</td></tr><tr><td>李四</td><td>32</td><td>上海</td></tr></table>",
        "type": "表格"
    }
]

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

for item in json_data:
    content = item["content"]
    item_type = item["type"]

    # 创建段落并应用常用格式
    builder.insert_paragraph()
    builder.paragraph_format.space_before = 0
    builder.paragraph_format.space_after = 6
    builder.font.size = 12

    if item_type == "普通段落":


        builder.paragraph_format.first_line_indent = 24  # 2 Chinese characters (approx 24 points)
        builder.paragraph_format.line_spacing = 12  # Single line spacing
        builder.write(content)


    elif item_type == "加粗段落":
        builder.paragraph_format.first_line_indent = 0
        if "患者编号" in content:
            builder.paragraph_format.style_name = "CBH2"
            builder.write(content)
        # 应用粗体格式
        else:
            builder.font.bold = True
            builder.write(content)
            builder.font.bold = False

    elif item_type == "表格":
        # 插入 HTML 表格
        builder.insert_html(content)

        # 格式化表头(如需要)
        tables = doc.get_child_nodes(aw.NodeType.TABLE, True)
        if tables.count > 0:
            table = tables[tables.count - 1].as_table()
            table.style_identifier = aw.StyleIdentifier.TABLE_GRID
            for cell in table.first_row.cells:
                cell = cell.as_cell()
                for para in cell.paragraphs:
                    para = para.as_paragraph()
                    for run in para.runs:
                        run = run.as_run()
                        run.font.size = 10

    builder.writeln()  # 在每个项目后添加换行符

doc.save("output.docx")

表格内容对其也不对啊。怎么参差不齐

我要设置这种表格样式。行高根据文字变化

@Tiaohh 您提供的设置 "CBH2 "样式名称的代码是正确的,但您需要确定该样式与段落相关。 您可以在打开样式的 "修改 "窗口时进行检查:

对于表格,您可以这样更新代码:

elif item_type == "表格":
    # 插入 HTML 表格
    builder.insert_html(content)

    # 格式化表头(如需要)
    tables = doc.get_child_nodes(aw.NodeType.TABLE, True)
    if tables.count > 0:
        table = tables[tables.count - 1].as_table()
        table.style_identifier = aw.StyleIdentifier.TABLE_GRID
        table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW)
        for cell in table.first_row.cells:
            cell = cell.as_cell()
            cell.cell_format.shading.background_pattern_color = aspose.pydrawing.Color.light_gray
            for para in cell.paragraphs:
                para = para.as_paragraph()
                para.paragraph_format.alignment = aw.ParagraphAlignment.LEFT
                for run in para.runs:
                    run = run.as_run()
                    run.font.size = 10

我这里怎么没有。需要怎么修改

@Tiaohh 如果您当前的文档没有这种样式,有几种方法可以添加这种样式:

  1. 您可以通过 Document.copy_styles_from_template method | Aspose.Words for Python 复制文档中的样式。

  2. 您可以修改 DOCX 基本模板并添加此样式,这样它就可以默认存在于每个新文档中了

  3. 您可以使用 Style Class | Aspose.Words for .NET 添加新配置的样式,并将其用于您需要的段落。

还是不对我添加了 他不是标题啊

正常应该是这样的

@Tiaohh 您需要配置您的样式,然后将其添加到段落中:

style = doc.styles.add(aw.StyleType.PARAGRAPH, "CBH2")
style.font.bold = True
builder.paragraph_format.style = style

wei shen m为什么表格后面多了一个换行

for item in json_data:
    content = item["content"]
    item_type = item["type"]

    # 创建段落并应用常用格式
    builder.insert_paragraph()
    builder.paragraph_format.space_before = 0
    builder.paragraph_format.space_after = 6
    builder.font.size = 12
    builder.paragraph_format.style_name = "Normal"

    if item_type == "普通段落" or item_type == "表注":
        if "研究分组" in content:
            builder.write(content)
        else:
            if item_type == "表注":
                builder.write(content)
            else:
                builder.paragraph_format.first_line_indent = 24  # 2 Chinese characters (approx 24 points)
                builder.paragraph_format.line_spacing = 12  # Single line spacing
                builder.write(content)


    elif item_type == "加粗段落":
        builder.paragraph_format.first_line_indent = 0
        builder.font.bold = True
        if "患者编号" in content:
            builder.paragraph_format.style_name = "CBH2"

            builder.write(content)

        # 应用粗体格式
        else:
            builder.write(content)
        builder.font.bold = False
    elif item_type == "表格":
        # 插入 HTML 表格
        builder.insert_html(content)

        # 格式化表格
        tables = doc.get_child_nodes(aw.NodeType.TABLE, True)
        if tables.count > 0:
            table = tables[tables.count - 1].as_table()
            table.style_identifier = aw.StyleIdentifier.TABLE_GRID
            table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_WINDOW)
            # 遍历所有行和单元格
            for i, row in enumerate(table.rows):
                row = row.as_row()
                for cell in row.cells:
                    cell = cell.as_cell()

                    # 只为表头(第一行)设置背景色
                    if i == 0:
                        cell.cell_format.shading.background_pattern_color = aspose.pydrawing.Color.light_gray

                    # 为所有单元格设置段落和字体格式
                    for para in cell.paragraphs:
                        para = para.as_paragraph()
                        para.paragraph_format.alignment = aw.ParagraphAlignment.LEFT
                        for run in para.runs:
                            run = run.as_run()
                            run.font.size = 10.5
                            run.font.name = "Times New Roman"  # 设置西文是新罗马字体
                            run.font.name_far_east = "宋体"


for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    paragraph = paragraph.as_paragraph()
    for run in paragraph.runs:
        run = run.as_run()
        # 设置所有文本运行的字体
        run.font.name = "Times New Roman"  # 设置西文是新罗马字体
        run.font.name_far_east = "宋体"  # 设置中文是宋体

doc.save("./test_template.docx")

@Tiaohh Aspose.Words 可模仿 MS Word 的行为,在插入 HTML 时,默认情况下也会插入段落。 您可以更新代码,使其在插入表格之前不添加额外的段落:

for item in json_data:
    content = item["content"]
    item_type = item["type"]

    prev_node = builder.current_paragraph.previous_sibling
    if prev_node is None or (prev_node is not None and prev_node.node_type != aw.NodeType.TABLE):
        builder.insert_paragraph()
    builder.paragraph_format.space_before = 0
    builder.paragraph_format.space_after = 6
    builder.font.size = 12