怎么获取docx文档里面大纲级别样式

@Tiaohh 您可以使用 ParagraphFormat.outline_level 属性指定或获取段落的大纲级别。

python自带的大纲级别样式是用的什么属性呢

大纲已经存在大纲级别了。不需要设置级别 只需要读取大纲级别的样式信息

@alexey.noskov

@Tiaohh 您可以使用相同的属性来获取现有段落的大纲级别值。

可是获取不到啊??????

@Tiaohh 您能否提供您的示例文档和代码,以便我们重现该问题? 另外,请指定预期的输出是什么。

for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    try:
        node = paragraph.as_paragraph()
        print(node.get_text())
        level = 1
        if node.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5, 6]:
            level = int(node.paragraph_format.outline_level) + 1
            if level > current_level:
                # 如果级别更深,将当前标题添加到堆栈
                stack.append((current_level, data))
                data = []
                current_level = level
            elif level < current_level:
                # 如果级别更浅,将堆栈中的项添加回数据
                while stack and stack[-1][0] >= level:
                    old_level, old_data = stack.pop()
                    data = old_data + data
                    current_level = old_level
            data.append(
                {
                    "Title": node.get_text(),
                    "Content": [],
                    "Level": level,
                    "Table": [],
                    "Tbale_name": [],
                }
            )
        else:
            if data:
                if node.get_text().startswith("表"):
                    if (
                        "Bullet" in node.paragraph_format.style.name
                        or "Caption" in node.paragraph_format.style.name
                    ):
                        data[-1]["Tbale_name"].append(" - " + node.get_text())
                    else:
                        data[-1]["Tbale_name"].append(node.get_text())
                if (
                    node.get_text().startswith("表")
                    or node.get_text().startswith("来源:")
                    or node.get_text().startswith("图")
                ):
                    pass
                else:

                    data[-1]["Content"].append(node.get_text())

    except Exception as e:
        print(e)

    while stack:
        old_level, old_data = stack.pop()
        data = old_data + data

你好帮我看一下为什么表格内容也读取出来了,不需要表格内容呀

@Tiaohh doc.get_child_nodes(aw.NodeType.PARAGRAPH, True) 返回文档中的所有段落,包括表格内的段落。 如果您需要跳过表格中的段落,可以使用代码 like this:

for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    if paragraph.get_ancestor(NodeType.TABLE) == None :
        # here will be paragraphs which are not inside tables.

请参阅我们的文档以了解有关 Aspose.Words 文档对象模型的更多信息:
https://docs.aspose.com/words/python-net/aspose-words-document-object-model/

不对呀。我按照你的写法表格数据还是存在data里面

lic = aw.License()
lic_path = os.path.join(settings.BASE_PATH, "core/Aspose.Total.Product.Family.lic")
lic.set_license(lic_path)
file = os.path.join(settings.DOCX_TEMPLATE_PATH, "189 phase 1 CSR-20220628-clean.docx")
doc = aw.Document(file)
current_level = 0
data = []
stack = []
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    try:
        node = paragraph.as_paragraph()

        level = 1
        if node.get_ancestor(aw.NodeType.TABLE) == None:
            if node.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5]:
                level = int(node.paragraph_format.outline_level) + 1
                if level > current_level:
                    # 如果级别更深,将当前标题添加到堆栈
                    stack.append((current_level, data))
                    data = []
                    current_level = level
                elif level < current_level:
                    # 如果级别更浅,将堆栈中的项添加回数据
                    while stack and stack[-1][0] >= level:
                        old_level, old_data = stack.pop()
                        data = old_data + data
                        current_level = old_level
                data.append(
                    {
                        "Title": node.get_text(),
                        "Content": [],
                        "Level": level,
                        "Table": [],
                        "Tbale_name": [],
                    }
                )
            else:
                if data:
                    if node.get_text().startswith("表"):
                        if (
                            "Bullet" in node.paragraph_format.style.name
                            or "Caption" in node.paragraph_format.style.name
                        ):
                            data[-1]["Tbale_name"].append(node.get_text().strip())
                    if (
                        node.get_text().startswith("表")
                        or node.get_text().startswith("来源:")
                        or node.get_text().startswith("图")
                    ):
                        pass
                    else:

                        data[-1]["Content"].append(node.get_text())

    except Exception as e:
        print(e)

while stack:
    old_level, old_data = stack.pop()
    data = old_data + data

@Tiaohh 您能否在此处附上您的输入文档以进行测试? 我们将检查该问题并为您提供更多信息。

for run in paragraph.runs:
    run = run.as_run()
    run.font.name = "Times New Roman"  # 设置西文是新罗马字体
    run.font.name_far_east = "宋体"
    run.font.size = 8 

写去的内容怎么设置成ERIS正文呢

new_paragraph = Paragraph(doc_main)
new_paragraph.paragraph_format.alignment = (aw.ParagraphAlignment.LEFT)
new_paragraph.paragraph_format.style.name = ("ERIS正文")
new_paragraph.paragraph_format.character_unit_first_line_indent = 2
new_run = Run(doc_main)
# new_run.text = i + aw.ControlChar.PARAGRAPH_BREAK_CHAR
new_run.text = i.strip()
new_run.font.name = "Times New Roman"  # 设置西文是新罗马字体
new_run.font.name_far_east = "宋体"
new_run.font.size = 12
new_paragraph.append_child(new_run)
# new_paragraph.paragraph_format.alignment = aw.ParagraphAlignment.LEFT
para.parent_node.insert_after(new_paragraph, para) 

这样设置可以吗

new_paragraph.paragraph_format.style.name = ("ERIS正文")

@Tiaohh 您能说得更具体一些吗? 不幸的是,目前尚不清楚问题是什么以及您想要实现什么目标。

谢谢已经实现了 我想要设置行间距为1.5倍怎么设置呢

@Tiaohh 您应该指定 LineSpacingRule.MULTIPLE 并将行间距值指定为 18:

# Set spacing as a multiple of default line spacing, which is 12 points by default.
# This kind of spacing will scale to different font sizes.
paragraph.paragraph_format.line_spacing_rule = aw.LineSpacingRule.MULTIPLE
paragraph.paragraph_format.line_spacing = 1.5*12

请参阅我们的文档以获取更多信息:
https://reference.aspose.com/words/python-net/aspose.words/paragraphformat/line_spacing_rule/

new_paragraph.paragraph_format.line_spacing_rule = aw.LineSpacingRule.MULTIPLE
new_paragraph.paragraph_format.line_spacing = 18

这样可以吗

段后样式怎么设置为0?????

@Tiaohh 你的意思是后面有空格吗? 如果是这样,您可以使用 ParagraphFormat.space_after 属性