使用python处理rtf,将表中的xxx的数字的数据行,背景颜色高亮,修改页眉页尾的数据

 一、rtf文件转为docx

使用word将rtf转为docx,或者代码转换也可

二、处理docx


from docx import Document
from docx.oxml.ns import qn, nsdecls
from docx.oxml import parse_xml, OxmlElement
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT


def doc_table_highlight_txt(cell, color):
    """
    高亮单元格中的文字
    Args:
        cell:
        color:

    Returns:

    """
    shade_obj = OxmlElement('w:shd')
    shade_obj.set(qn('w:fill'), color)

    if cell._element.xpath('.//w:rPr'):
        cell._element.xpath('.//w:rPr')[0].append(shade_obj)
    else:
        a = cell.text
        cell._element.clear_content()
        p = cell.add_paragraph()
        r = p.add_run(a)
        rpr_obj = OxmlElement('w:rPr')
        rpr_obj.append(shade_obj)
        r._element.append(rpr_obj)



def highlight_text_in_docx(doc, text_to_highlight, highlight_color):

        # 遍历文档中的所有表格
        for table in doc.tables:
            for row_idx, row in enumerate(table.rows):
                # 检查第一列的值是否匹配目标数字
                if str(text_to_highlight) == row.cells[0].text:
                    # 如果匹配,高亮整行
                    # shading_elm = parse_xml(r'<w:val="clear" w:color="auto" w:fill="{}"/>'.format(highlight_color))

                    for cell in row.cells:
                        part = cell._element
                        # rPr = part.get_or_add_rPr()
                        # rPr.append(shading_elm)
                        # cell._tc.get_or_add_tcPr().append(shading_elm)  # 将shading_elm添加到单元格的属性中
                        # highlight_color = cell.font.highlight_color
                        # cell._tc.get_or_add_tcPr().append(shading_elm)  # 将shading_elm添加到单元格的属性中
                        # doc_table_highlight_txt(cell, highlight_color)
                        color = parse_xml(r'<w:shd {} w:fill="{color_value}"/>'.format(
                            nsdecls('w'), color_value=highlight_color))
                        cell._tc.get_or_add_tcPr().append(color)


def modify_header_footer(section, header_text, footer_text):
    header = section.header
    footer = section.footer

    # 清除原有的页眉页脚内容
    # header.clear_content()
    # footer.clear_content()

    # 添加新的页眉页脚内容
    header.add_paragraph(header_text)
    footer.add_paragraph(footer_text)

# 打开DOCX文件
doc_path = 'D:\\Documents\\WeChat Files\\wxid_ate5y3ynxm3b22\\FileStorage\\File\\2024-04\\Monthly Summary_Monthly.docx'
doc = Document(doc_path)

# 高亮包含特定数字的数据行
highlight_text_in_docx(doc, '1031', 'FFFF00')  # 假设'xxx'是要查找的数字,'FFFF00'是黄色

# 假设我们编辑第一个节的页眉和页脚
section = doc.sections[0]
modify_header_footer(section, 'New Header Content', 'New Footer Content')

# 保存修改后的DOCX文件
doc.save('output.docx')

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颖火虫-IT赵云

您的鼓励就是对我最大的进步!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值