读取docx 怎么把文档所有数字变成加粗样式

读取docx 怎么把文档所有数字变成加粗样式

@hhh1111 您需要使用 font.bold

我只需要把数字进行加粗样式

@hhh1111 没有简单的解决方法。您可以尝试使用以下代码更改字体编号。

doc = aw.Document("input.docx")
all_numbers = []

for para in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    para = para.as_paragraph()
    para_text = para.to_string(aw.SaveFormat.TEXT)
    pattern = re.compile(r"(?:\d*\.*\d+)")
    numbers = list(re.findall(pattern, para_text))
    for number in numbers:
        all_numbers.append(number)

all_numbers = list(dict.fromkeys(all_numbers))

opt = aw.replacing.FindReplaceOptions()
opt.match_case = True
opt.find_whole_words_only = True
for number in all_numbers:
    doc.range.replace(number, number, opt)

for run in doc.get_child_nodes(aw.NodeType.RUN, True):
    run = run.as_run()
    if any(run.text in s for s in all_numbers):
        run.font.bold = True

doc.save("output.docx")