admin管理员组

文章数量:1558057

高亮显示word中指定字体颜色,用python实现比较麻烦,用word 宏实现更容易,效率也更高。当然python扩展性能更好,所以也尝试用python实现。

由于python-docx插件需要分开段落和表格读取,应此需要分开操作。段落的处理速度比较快,小型表格处理也还可以,几百行到上千行的就很慢了。

首先安装Python支持word插件,然后导入。

from docx import Document
from docx.shared import RGBColor, Pt
from docx.enum.text import WD_COLOR_INDEX

调用方式:

file = Document("C:/xx.doc") 
paragraphs_utils(file,"你","paragraphs")

具体功能实现,对于表格就是读取其中的cell,再运行一遍查找就可以了。

# highlight关键字的所有自然段
def sub_highlight(par_obj, replaceword, str_type):  # 自然段遍历程序
    global all_length
    for p in par_obj.paragraphs:  # 遍历所有自然段
        if str_type == "paragraphs":  # 区别段落和表格,表格直接处理
            all_length = all_length + 1
            progress_bar()
        if replaceword in p.text:  # print(file.paragraphs[i].text)加快速度
            for r in p.runs: 

本文标签: 颜色文字Python高亮word