python-docx在word文件表格中指定行下插入新一行并填充值

 

from docx import Document
from copy import deepcopy

def insert_row_after_specific_value(doc, table_index, column_header, target_value, new_row_data):
    # 加载文档
    # doc = doc_path

    # 检查表格索引是否有效
    if table_index >= len(doc.tables):
        print("文档中没有足够的表格。")
        return

        # 获取指定表格
    table = doc.tables[table_index]

    # 找到列标题“单位类别”的索引
    column_index = None
    for i, cell in enumerate(table.rows[0].cells):
        if cell.text.strip() == column_header:
            column_index = i
            break

    if column_index is None:
        print("未找到指定的列标题:", column_header)
        return

        # 遍历表格中的行,找到符合条件的行
    insert_index = None
    for i, row in enumerate(table.rows):
        if i == 0:  # 跳过标题行
            continue
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值