那我设置标题一呢代码要怎么修改呢
还要一种情况是有针对该事件的治疗药物包括 也需要根据加粗的一级标题进行查询下面的内容
您好怎么样 有结果啦吗?????
好的谢谢 ,希望您可以测试全一点,以上2种情况已经描述了
他出现的段落已经是固定的就在我截图的那个段落里面
@hhh1111 请尽量按照黑体样式使用代码。如果第一段中没有任何内容,它还会在段末插入句号。
def add_comments_sae_drug_name(file_path, para_text_list):
doc = aw.Document(file_path)
buider = aw.DocumentBuilder(doc)
# 查找文档中的特定文本并添加批注
for item in para_text_list:
for key, value in item.items():
for k, v in value.items():
is_comment_added = False
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(k, "$0", opt)
comment = aw.Comment(doc, '', "", datetime.now())
comment.set_text(v)
# 查找文档中的特定文本并添加批注
for top_run in doc.get_child_nodes(aw.NodeType.RUN, True):
top_run = top_run.as_run()
if top_run.text == key and top_run.font.bold is True:
top_para = top_run.parent_paragraph
if top_para.get_ancestor(aw.NodeType.HEADER_FOOTER):
continue
next_para = top_para.next_sibling
while next_para is not None:
next_para = next_para.as_paragraph()
for run in next_para.runs:
run = run.as_run()
if run.text == k:
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
next_para.insert_before(comment_start, run)
next_para.insert_after(comment_end, run)
next_para.insert_after(comment, run)
is_comment_added = True
break
if is_comment_added:
break
next_para = next_para.next_sibling
if not is_comment_added:
buider.move_to(top_para.next_sibling)
buider.write("。")
# 保存文档
doc.save(file_path)
return file_path
image.jpg (233.2 KB)
还是不太对
add_comments_sae_drug_name(“S101001-感染性肺炎, 窦性心动过缓.rtf”, [{“感染性肺炎”: {“该事件结局为”: “插入批注的内容”}},
{“窦性心动过缓”: {
“针对该事件的治疗药物或操作包括”: “插入批注的内容”}}])
感染性肺炎加粗字体下面没有该事件结局为的内容。批注应该插入到第一段内容句号的位置
image.png (117.5 KB)
image.png (275.6 KB)
为什么wps打开插入的位置是正常的 用word打开批注就乱了
image.jpg (126.9 KB)
S101001-窦性心动过缓, 感染性肺炎.docx (369.1 KB)
@hhh1111 使用此代码在第一段的句号处插入注释。
def add_comments_sae_drug_name(file_path, para_text_list):
doc = aw.Document(file_path)
# 查找文档中的特定文本并添加批注
for item in para_text_list:
for key, value in item.items():
for k, v in value.items():
is_comment_added = False
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(k, "$0", opt)
comment = aw.Comment(doc, '', "", datetime.now())
comment.set_text(v)
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 查找文档中的特定文本并添加批注
for top_run in doc.get_child_nodes(aw.NodeType.RUN, True):
top_run = top_run.as_run()
if top_run.text == key and top_run.font.bold is True:
top_para = top_run.parent_paragraph
if top_para.get_ancestor(aw.NodeType.HEADER_FOOTER):
continue
next_para = top_para.next_sibling
while next_para is not None:
next_para = next_para.as_paragraph()
for run in next_para.runs:
run = run.as_run()
if run.text == k:
# 插入批注范围和批注
next_para.insert_before(comment_start, run)
next_para.insert_after(comment_end, run)
next_para.insert_after(comment, run)
is_comment_added = True
break
if is_comment_added:
break
next_para = next_para.next_sibling
if not is_comment_added:
next_para = top_para.next_sibling.as_paragraph()
i = 0
while next_para.runs[i] is not None:
current_run = next_para.runs[i]
i += 1
after_run = current_run.clone(True)
current_run.parent_node.insert_after(after_run, current_run)
after_run = after_run.as_run()
after_run.text = current_run.text[-1]
current_run.text = current_run.text[0:len(current_run.text) - 1]
next_para.insert_before(comment_start, after_run)
next_para.insert_after(comment_end, after_run)
next_para.insert_after(comment, after_run)
# 保存文档
doc.save(file_path)
return file_path