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