import docx import re def b_need_highlight(word: str) -> bool: merge = ["me","you","him"] #这里是需要高亮的词汇,可以随个人需求改动 if word in merge: return True def high_light(): #进行高亮显示部分 original_doc = docx.Document("D:\input.docx") new_doc = docx.Document() for orig_para in original_doc.paragraphs: print(orig_para.runs.__len__()) print(orig_para.text) new_para = new_doc.add_paragraph() if orig_para.runs.__len__() != 0: res = re.findall(r"\w+|[^\w\s]", orig_para.text,re.I) print("The list of words is : ",res) for i in res: print(i) run = new_para.add_run(i + " ") result = b_need_highlight(i) if result == True: run.font.color.rgb = docx.shared.RGBColor(238, 75, 43) new_doc.save("D:\output.docx") #这里是将高亮处理后保存到新的文件 if __name__ == '__main__': high_light()
python-docx文档高亮显示
最新推荐文章于 2023-09-17 17:21:40 发布