怎么给指定标题最下面插入内容呢 标题下面有内容我需要插入在标题最后面
@Tiaohh 一个简单的方法是找到所需的段落,然后获取文本并将文本添加到标题末尾。 就像这样
doc = aw.Document("input.docx")
title_para = doc.first_section.body.first_paragraph
paragraphs = doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)
below_text = ""
for para in paragraphs:
if "below" in para.get_text():
below_text = para.get_text()
para.remove()
break
run = aw.Run(doc)
run.text = " " + below_text
title_para.runs.add(run)
doc.save("output.docx")
不对我需要查找标题 插入到标题最下面
不是 标题是黑色的 比如10.1 受试者分布
@Tiaohh 好的,您需要在标题前添加内容。 您可以使用以下代码:
doc = aw.Document("input.docx")
# Find all paragraphs containing the search text
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
if "14.1.1.1" in paragraph.get_text():
# Create a new paragraph with the content to insert
new_paragraph = aw.Paragraph(doc)
new_run = aw.Run(doc, "New content")
new_paragraph.append_child(new_run)
# Insert before the found paragraph
paragraph.parent_node.insert_before(new_paragraph, paragraph)
doc.save("output.docx")
您可以使用 CompositeNode.insert_before method | Aspose.Words for Python 或 CompositeNode.insert_after method | Aspose.Words for Python 在需要的位置插入内容。
def insert_content_after_chapter(file_path, output_path, target_chapter, content_to_insert):
doc = aw.Document(file_path)
# Find all paragraphs containing the search text
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
if "10" in paragraph.get_text():
# Create a new paragraph with the content to insert
new_paragraph = aw.Paragraph(doc)
new_run = aw.Run(doc, " 1. 患者年龄分布较广,但主要集中在儿童和青少年群体,中位年龄为12岁。")
new_paragraph.append_child(new_run)
# Insert before the found paragraph
paragraph.parent_node.insert_before(new_paragraph, paragraph)
doc.save("output.docx")
不对我在第10章节 插入怎么跑地11下面的章节里面了
def insert_content_after_chapter(file_path, output_path, target_chapter, content_to_insert):
doc = aw.Document(file_path)
# Find all paragraphs containing the search text
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
if "10.1" in paragraph.get_text():
# Create a new paragraph with the content to insert
new_paragraph = aw.Paragraph(doc)
new_run = aw.Run(doc, "我是哈哈哈哈哈")
new_paragraph.append_child(new_run)
# Insert before the found paragraph
paragraph.parent_node.insert_before(new_paragraph, paragraph)
doc.save("output.docx")
我在10.1插入 怎么插入到表格里面 了
请回复一下????????????
import json
import os
import re
import aspose.words as aw
# 激活 Aspose.Words 许可证
try:
lic = aw.License()
lic_path = "../Aspose.Total.Product.Family.lic"
lic.set_license(lic_path)
except:
print("警告:许可证加载失败,使用评估模式")
def is_heading(paragraph):
"""判断段落是否为标题"""
if paragraph.node_type == aw.NodeType.PARAGRAPH:
para = paragraph.as_paragraph()
# 检查段落的样式
if para.paragraph_format.style:
style_name = para.paragraph_format.style.name
# 检查样式名称是否为标题样式
if style_name.startswith("Heading") or "标题" in style_name or para.paragraph_format.outline_level in [0, 1,
2, 3,
4,
5]:
return True
# 如果没有特定的样式,可以检查其他特征
# 例如:段落是否有编号格式(如"1.1", "1.2"等)
text = para.get_text().strip()
if re.match(r"^\d+(\.\d+)*\s", text): # 匹配如"1.", "1.1.", "1.1.1."等格式
return True
return False
def is_in_table(node):
"""检查节点是否在表格内"""
return node.get_ancestor(aw.NodeType.TABLE) is not None
def insert_content_after_chapter(file_path, output_path, target_chapter, content_to_insert):
doc = aw.Document(file_path)
# 获取所有非表格内的段落
paragraphs = [p for p in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True) if not is_in_table(p)]
# 查找包含目标文本的段落
target_index = -1
for i, paragraph in enumerate(paragraphs):
if target_chapter in paragraph.get_text():
target_index = i
break
# 如果找到目标段落,从下一个段落开始查找标题
if target_index >= 0:
next_heading = None
for i in range(target_index + 1, len(paragraphs)):
if is_heading(paragraphs[i]):
next_heading = paragraphs[i]
break
# 如果找到了下一个标题
if next_heading:
# 查找标题前的段落
previous_paragraph = None
current_node = next_heading.previous_sibling
while current_node is not None:
if current_node.node_type == aw.NodeType.PARAGRAPH and not is_in_table(current_node):
previous_paragraph = current_node.as_paragraph()
break
current_node = current_node.previous_sibling
# 如果找到了前一个段落且它有分节符
if previous_paragraph and previous_paragraph.has_end_of_section:
# 创建新段落
new_paragraph = aw.Paragraph(doc)
new_run = aw.Run(doc, content_to_insert)
new_paragraph.append_child(new_run)
# 获取段落的所有运行对象(runs)
runs = [r for r in previous_paragraph.get_child_nodes(aw.NodeType.RUN, True)]
# 创建一个新段落,用于保存原段落的内容
temp_paragraph = aw.Paragraph(doc)
# 复制原段落的格式
temp_paragraph.paragraph_format.clone_from(previous_paragraph.paragraph_format)
# 复制原段落的内容
for run in runs:
temp_paragraph.append_child(run.clone(True))
# 保留原段落的分节符属性
temp_paragraph.has_end_of_section = previous_paragraph.has_end_of_section
# 在原段落后面插入新段落和临时段落
parent = previous_paragraph.parent_node
parent.insert_after(new_paragraph, previous_paragraph)
parent.insert_after(temp_paragraph, new_paragraph)
# 移除原段落的分节符
previous_paragraph.has_end_of_section = False
# 移除原段落末尾的段落结束符(如果有的话)
if previous_paragraph.get_text().endswith("\r"):
previous_paragraph.get_child_nodes(aw.NodeType.RUN, True)[-1].text = \
previous_paragraph.get_child_nodes(aw.NodeType.RUN, True)[-1].text.rstrip("\r")
else:
# 如果前一个段落没有分节符,或者找不到前一个段落,就在标题前插入
new_paragraph = aw.Paragraph(doc)
new_run = aw.Run(doc, content_to_insert)
new_paragraph.append_child(new_run)
next_heading.parent_node.insert_before(new_paragraph, next_heading)
doc.save(output_path)
# 主程序
if __name__ == "__main__":
# 文件路径
input_file = "./上海健信 10-12章 intext_20250415-客户回复-DIP已修改待确认-JX0417.docx"
output_file = "./result_with_content.docx"
# 目标章节标题或编号
target_chapter = "方案偏离"
# 要插入的内容
content = """
基于上述受试者情况分析,本研究纳入的患者具有以下特点:
1. 患者年龄分布较广,但主要集中在儿童和青少年群体,中位年龄为12岁。
2. 大多数患者(85.7%)曾接受过多线治疗,其中64.3%的患者既往接受过异基因造血干细胞移植。
3. 所有患者均属于难治/复发性B-ALL,并且预后较差,常规治疗手段已耗尽。
4. 患者基线时血液学及生化指标普遍异常,反映了晚期血液系统恶性肿瘤的临床特征。
总体而言,本研究入组的患者群体代表了临床实践中最具挑战性的B-ALL患者群体,这些患者迫切需要新型有效的治疗手段。
"""
# 执行插入
insert_content_after_chapter(input_file, output_file, target_chapter, content)
目前插入不太对
怎么到这个位置前面一个段落分节符之前
研究对接受FKC889治疗的受试者在试验期间的实验室检查指标变化进行了分析,包括全血细胞计数及分类、血生化指标、炎性标志物、骨髓原始或幼稚淋巴细胞百分比等指标。对各项实验室检查指标在各访视时间点的实际测量值及相对于基线的变化值进行了描述性统计分析,并对基线与基线后最严重临床意义变化情况进行了交叉分类汇总。此外,还特别关注了与FKC889治疗相关的实验室检查异常情况,依据NCI CTCAE 5.0版标准对贫血、白细胞计数降低、中性粒细胞计数降低、血小板计数降低、淋巴细胞计数降低、肝功能指标(ALT、AST、ALP、胆红素)、肾功能指标(肌酐)、电解质异常(钠、钾、镁)、血糖降低等指标的异常情况进行了分级评估,并汇总了基线与基线后最严重CTCAE分级的变化情况。\n\n总体而言,FKC889治疗后观察到部分受试者出现了不同程度的血液学异常,包括贫血、白细胞减少、中性粒细胞减少、血小板减少和淋巴细胞减少等,部分受试者出现了肝功能指标升高、电解质紊乱等生化指标异常。这些实验室检查异常大多为一过性,经对症支持治疗后可恢复至基线水平或临床可接受范围内。未观察到因实验室检查异常导致的死亡事件。\n\n具体实验室检查指标的详细数据及变化情况参见表XX。
目前2个问题
1、插入位置不对
怎么到这个位置前面一个段落分节符之前
2上面是我传入的内容 会有一些, 的标签 我需要设置对应的格式
@Tiaohh 在您提供的文档中,在目标标题之后没有任何其他标题,因此您的代码会将 next_heading
返回为 None
,从而跳过其他代码。 此外,您在代码中使用了 has_end_of_section
,这不是我们 API 的一部分。 要保留段落格式,可以使用 DocumentBuilder
。 因此,我们的主要目标是找到可以开始插入段落的正确段落。 尝试使用此代码在所需标题后的末尾插入段落:
content = "研究对接受FKC889治疗的受试者在试验期间的实验室检查指标变化进行了分析,包括全血细胞计数及分类、血生化指标、炎性标志物、骨髓原始或幼稚淋巴细胞百分比等指标。" \
"对各项实验室检查指标在各访视时间点的实际测量值及相对于基线的变化值进行了描述性统计分析,并对基线与基线后最严重临床意义变化情况进行了交叉分类汇总。" \
"此外,还特别关注了与FKC889治疗相关的实验室检查异常情况,依据NCI CTCAE 5.0版标准对贫血、白细胞计数降低、中性粒细胞计数降低、血小板计数降低、淋巴细胞计数降低、肝功能指标(ALT、AST、ALP、胆红素)、肾功能指标(肌酐)、电解质异常(钠、钾、镁)、血糖降低等指标的异常情况进行了分级评估,并汇总了基线与基线后最严重CTCAE分级的变化情况。\n" \
"总体而言,FKC889治疗后观察到部分受试者出现了不同程度的血液学异常,包括贫血、白细胞减少、中性粒细胞减少、血小板减少和淋巴细胞减少等,部分受试者出现了肝功能指标升高、电解质紊乱等生化指标异常。这些实验室检查异常大多为一过性,经对症支持治疗后可恢复至基线水平或临床可接受范围内。未观察到因实验室检查异常导致的死亡事件。\n" \
"具体实验室检查指标的详细数据及变化情况参见表XX。"
doc = aw.Document("input.docx")
builder = aw.DocumentBuilder(doc)
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
if "方案偏离" in paragraph.get_text() and self.is_heading(paragraph):
next_para = paragraph.next_sibling
while next_para is not None and not self.is_heading(next_para):
if next_para.next_sibling is not None:
next_para = next_para.next_sibling
else:
break
if self.is_heading(next_para):
next_para = next_para.previous_sibling
builder.move_to(next_para)
builder.writeln()
builder.write(content)
doc.save("output.docx")
还有一个问题 我文档里面的有序列表读取不出来
@Tiaohh 您可以改进这段代码,通过列表编号值获取标题,并获取表格标题前的段落:
content = "研究对接受FKC889治疗的受试者在试验期间的实验室检查指标变化进行了分析,包括全血细胞计数及分类、血生化指标、炎性标志物、骨髓原始或幼稚淋巴细胞百分比等指标。" \
"对各项实验室检查指标在各访视时间点的实际测量值及相对于基线的变化值进行了描述性统计分析,并对基线与基线后最严重临床意义变化情况进行了交叉分类汇总。" \
"此外,还特别关注了与FKC889治疗相关的实验室检查异常情况,依据NCI CTCAE 5.0版标准对贫血、白细胞计数降低、中性粒细胞计数降低、血小板计数降低、淋巴细胞计数降低、肝功能指标(ALT、AST、ALP、胆红素)、肾功能指标(肌酐)、电解质异常(钠、钾、镁)、血糖降低等指标的异常情况进行了分级评估,并汇总了基线与基线后最严重CTCAE分级的变化情况。\n" \
"总体而言,FKC889治疗后观察到部分受试者出现了不同程度的血液学异常,包括贫血、白细胞减少、中性粒细胞减少、血小板减少和淋巴细胞减少等,部分受试者出现了肝功能指标升高、电解质紊乱等生化指标异常。这些实验室检查异常大多为一过性,经对症支持治疗后可恢复至基线水平或临床可接受范围内。未观察到因实验室检查异常导致的死亡事件。\n" \
"具体实验室检查指标的详细数据及变化情况参见表XX。"
doc = aw.Document("input.docx")
builder = aw.DocumentBuilder(doc)
doc.update_list_labels()
for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
paragraph = paragraph.as_paragraph()
if paragraph.is_list_item:
if "10.1" in paragraph.list_label.label_string and self.is_heading(paragraph):
next_para = paragraph.next_sibling
while next_para:
if next_para.node_type == aw.NodeType.TABLE:
next_para = next_para.previous_sibling.previous_sibling
break
elif self.is_heading(next_para):
next_para = next_para.previous_sibling
break
if next_para.next_sibling is not None:
next_para = next_para.next_sibling
else:
break
builder.move_to(next_para)
builder.writeln()
builder.write(content)
doc.save("output.docx")