docx 怎么给具体文本添加批注信息呢
def main():
# 创建一个新文档
doc = aw.Document('')
builder = aw.DocumentBuilder(doc)
# 写入文本 "aaaaaa"
builder.write("aaaaaa")
# 定义要查找的文本
text_to_find = "aa"
# 创建批注的作者和日期
author = "Your Name"
date = datetime.now()
# 创建一个批注
comment = aw.Comment(doc, author, "red", date)
comment.set_text("这是一个批注的示例。")
# 查找文档中的特定文本并添加批注
for run in doc.get_child_nodes(aw.NodeType.RUN, True):
run = run.as_run()
if text_to_find in run.get_text():
# 找到文本后,获取其索引范围
start_index = run.get_text().find(text_to_find)
end_index = start_index + len(text_to_find)
# 拆分Run对象,以便可以插入批注范围
if start_index > 0:
run = run.split(start_index)
run = run.split(end_index - start_index)
# 获取拆分后的Run对象
new_run = run.next_sibling
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
paragraph = run.parent_paragraph
paragraph.insert_before(comment_start, run)
paragraph.insert_after(comment_end, new_run)
paragraph.insert_after(comment, new_run)
break
# 保存文档
doc.save("document_with_comment.docx")这样写报错
@hhh1111 您可以使用 Range.replace 方法将搜索到的每个单词都变成单独的 Run 节点。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# 写入文本 "aaaaaa"
builder.write("aaaaaa")
# 定义要查找的文本
text_to_find = "aa"
# 创建批注的作者和日期
author = "Your Name"
date = datetime.now()
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(text_to_find, "$0", opt)
# 创建一个批注
comment = aw.Comment(doc, author, "red", date)
comment.set_text("这是一个批注的示例。")
# 查找文档中的特定文本并添加批注
for run in doc.get_child_nodes(aw.NodeType.RUN, True):
run = run.as_run()
if run.text == text_to_find:
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
paragraph = run.parent_paragraph
paragraph.insert_before(comment_start, run)
paragraph.insert_after(comment_end, run)
paragraph.insert_after(comment, run)
break
# 保存文档
doc.save("document_with_comment.docx")
def add_comments_utils(file_path, text_to_find, comment_text):
doc = aw.Document(file_path)
# 查找文档中的特定文本并添加批注
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(text_to_find, "$0", opt)
# 创建一个批注
comment = aw.Comment(doc, '', "", date)
comment.set_text(comment_text)
# 查找文档中的特定文本并添加批注
for run in doc.get_child_nodes(aw.NodeType.RUN, True):
run = run.as_run()
if run.text == text_to_find:
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
paragraph = run.parent_paragraph
paragraph.insert_before(comment_start, run)
paragraph.insert_after(comment_end, run)
paragraph.insert_after(comment, run)
break
# 保存文档
doc.save(file_path)
return file_path
为什么批注不能给数字添加呢
对第一个段落内容的第一个句号
@hhh1111 您可以找到带有该句号的段落,然后替换该句号。
text_to_find = "(入组年龄为75)。"
text_to_replace = "。"
doc = aw.Document("a.docx")
for para in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
para = para.as_paragraph()
if text_to_find in para.get_text():
# 查找文档中的特定文本并添加批注
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
para.range.replace(text_to_replace, "$0", opt)
# 创建一个批注
comment = aw.Comment(doc, '', "", datetime.now())
comment.set_text("This is a comment")
# 查找文档中的特定文本并添加批注
for run in doc.get_child_nodes(aw.NodeType.RUN, True):
run = run.as_run()
if run.text == text_to_replace:
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
paragraph = run.parent_paragraph
paragraph.insert_before(comment_start, run)
paragraph.insert_after(comment_end, run)
paragraph.insert_after(comment, run)
break
# 保存文档
doc.save("output.docx")
但是内容是不固定的 入组年龄为75)
@hhh1111 您可以使用这个简单的代码。通过在注释插入代码中使用 “break”,您可以在一个句号上添加注释。但是,如果需要在第二个句号上添加注释,则需要另一个锚点,这个锚点可以是段落文本。因此,如果您只需要在一个句号上添加注释,而没有其他信息,则无法实现。
text_to_find = "。"
doc = aw.Document("a.docx")
# 查找文档中的特定文本并添加批注
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(text_to_find, "$0", opt)
# 创建一个批注
comment = aw.Comment(doc, '', "", datetime.now())
comment.set_text("This is a comment")
# 查找文档中的特定文本并添加批注
for run in doc.get_child_nodes(aw.NodeType.RUN, True):
run = run.as_run()
if run.text == text_to_find:
# 创建批注范围
comment_start = aw.CommentRangeStart(doc, comment.id)
comment_end = aw.CommentRangeEnd(doc, comment.id)
# 插入批注范围和批注
paragraph = run.parent_paragraph
paragraph.insert_before(comment_start, run)
paragraph.insert_after(comment_end, run)
paragraph.insert_after(comment, run)
break
# 保存文档
doc.save("output.docx")
但是我用代码插入 用wps打开rtf文件就变了