还有就是怎么设置页面页脚的字体大小呢
@hhh1111 您可以使用以下代码。您可以删除未使用的页脚。
doc = aw.Document("input.docx")
for section in doc.sections:
section = section.as_section()
# 有三种页脚和页眉类型。
# 1 - "第一 "页眉/页脚,只出现在章节的第一页。
footer = section.headers_footers[aw.HeaderFooterType.FOOTER_FIRST]
if footer is not None:
self.change_run_font(footer)
# 2 - "主要 "页眉/页脚,出现在奇数页面上。
footer = section.headers_footers[aw.HeaderFooterType.FOOTER_PRIMARY]
if footer is not None:
self.change_run_font(footer)
# 3 - "偶数 "页眉/页脚,出现在偶数页面上。
footer = section.headers_footers[aw.HeaderFooterType.FOOTER_EVEN]
if footer is not None:
self.change_run_font(footer)
def change_run_font(self, footer):
runs = footer.get_child_nodes(aw.NodeType.RUN, True)
for run in runs:
run = run.as_run()
run.font.size = 8
@hhh1111 我检查了文档,发现它不能以 docx 格式在 MS Word 中打开,因为它是 RTF 文档。当我把".docx “改为”.rtf "后,我就可以打开这个文档了。也许 WPS 在读取 RTF 文档时存在某些错误。因为当我使用代码并将其保存为 docx 文件时,在 WPS 中可以正确显示。
好的谢谢啦啦啦。 。。。。。
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)
# 创建一个批注
table = doc.get_child_nodes(aw.NodeType.TABLE, True)[3].as_table()
for row in table.rows:
for cell in row.as_row():
cell = cell.as_cell()
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
for run in paragraph.runs:
run = run.as_run()
run.font.name = "Times New Roman" # 设置西文是新罗马字体
run.font.name_far_east = "宋体"
run.font.size = 8
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 您可以这样修改代码,首先找到搜索到的段落:
def add_comments_utils(file_path, text_to_find, para_text, comment_text):
doc = aw.Document(file_path)
# 查找文档中的特定文本并添加批注
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(text_to_find, "$0", opt)
# 创建一个批注
table = doc.get_child_nodes(aw.NodeType.TABLE, True)[3].as_table()
for row in table.rows:
for cell in row.as_row():
cell = cell.as_cell()
for paragraph in cell.paragraphs:
paragraph = paragraph.as_paragraph()
for run in paragraph.runs:
run = run.as_run()
run.font.name = "Times New Roman" # 设置西文是新罗马字体
run.font.name_far_east = "宋体"
run.font.size = 8
comment = aw.Comment(doc, '', "", date)
comment.set_text(comment_text)
# 查找文档中的特定文本并添加批注
for find_para_run in doc.get_child_nodes(aw.NodeType.RUN, True):
find_para_run = find_para_run.as_run()
if find_para_run.text == para_text:
for run in find_para_run.parent_paragraph.runs:
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
def add_comments_sae_drug_name(file_path, text_to_find, para_text_list):
doc = aw.Document(file_path)
# 查找文档中的特定文本并添加批注
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(text_to_find, “$0”, opt)
for item in para_text_list:
for key, value in item.items():
comment = aw.Comment(doc, ‘’, “”, date)
comment.set_text(value)
# 查找文档中的特定文本并添加批注
for find_para_run in doc.get_child_nodes(aw.NodeType.RUN, True):
find_para_run = find_para_run.as_run()
if find_para_run.text == key:
for run in find_para_run.parent_paragraph.runs:
run = run.as_run()
print(run.text,‘222222’)
print()
print(text_to_find,‘1111111’)
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
add_comments_sae_drug_name(“S101001-感染性肺炎, 窦性心动过缓.rtf”, “针对该事件的治疗药物或操作包括”, [{“感染性肺炎”:“哌拉西林钠他唑巴坦钠、地塞米松磷酸钠”,“窦性心动过缓”:“药心宝丸”}])
请问为什么没有插入成功
并且还存在一个问题 我需要 在开始的位置为 感染性肺炎 到窦性心动过缓寻找第一个关键字出现的位置
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():
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace(key, "$0", opt)
for k, v in value.items():
comment = aw.Comment(doc, '', "", date)
comment.set_text(v)
# 查找文档中的特定文本并添加批注
for find_para_run in doc.get_child_nodes(aw.NodeType.RUN, True):
find_para_run = find_para_run.as_run()
if find_para_run.text == key:
for run in find_para_run.parent_paragraph.runs:
run = run.as_run()
if run.text == k:
# 创建批注范围
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
add_comments_sae_drug_name("S101001-感染性肺炎, 窦性心动过缓.rtf", [{"感染性肺炎": {"该事件结局为": "插入批注的内容"}}, {"窦性心动过缓": {"针对该事件的治疗药物包括": "插入批注的内容"}}])
代码 为什么没有插入批注。文件已经有相关的内容
该事件结局为 怎么修改成插入 感染性肺炎 下面的第一段结尾的句号位置呢
目前2个问题
1、通过代码插入不进去批注
2、该事件结局为 怎么修改成插入 感染性肺炎 下面的第一段结尾的句号位置呢
内容查找描述 首先根据感染性肺炎查询 在根据字典里面的键进行查找该事件结局为 值为插入批注的内容
add_comments_sae_drug_name(“S101001-感染性肺炎, 窦性心动过缓.rtf”, [{“感染性肺炎”: {“该事件结局为”: “插入批注的内容”}}, {“窦性心动过缓”: {“针对该事件的治疗药物包括”: “插入批注的内容”}}])
@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)
# 查找文档中的特定文本并添加批注
for find_para in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
find_para = find_para.as_paragraph()
if key in find_para.get_text() and find_para.paragraph_format.is_heading:
find_para = find_para.next_sibling
while find_para is not None:
find_para = find_para.as_paragraph()
for run in find_para.runs:
run = run.as_run()
if run.text == k:
# 创建批注范围
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)
is_comment_added = True
break
if is_comment_added:
break
find_para = find_para.next_sibling
# 保存文档
doc.save(file_path)
return file_path
另外,由于有很多相同的词,你需要改变标题的样式。现在只是普通样式和粗体。没有足够的锚点来找到这些段落。例如,你可以将顶部段落名称的样式设置为 “标题 3”。
可以通过加粗的样式来查找下面的第一个段落最后的句号吗
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)
# 查找文档中的特定文本并添加批注
for find_para in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
find_para = find_para.as_paragraph()
if key in find_para.get_text() and find_para.paragraph_format.is_heading:
find_para = find_para.next_sibling
while find_para is not None:
find_para = find_para.as_paragraph()
for run in find_para.runs:
run = run.as_run()
if run.text == k:
# 创建批注范围
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)
is_comment_added = True
break
if is_comment_added:
break
find_para = find_para.next_sibling
# 保存文档
doc.save(file_path)
return file_path
add_comments_sae_drug_name(“S101001-感染性肺炎, 窦性心动过缓.rtf”, [{“感染性肺炎”: {“该事件结局为”: “插入批注的内容”}},
{“窦性心动过缓”: {
“针对该事件的治疗药物包括”: “插入批注的内容”}}])没有插入进去呀