python小程序-把产品1中的需求移到产品2中,产品1中的用例需要导入产品2,则对应的需求id发生变化,需要进行替换

1、需求

把产品1中的需求移到产品2中,产品1中的用例需要导入产品2,则对应的需求id发生变化,需要进行替换

2、步骤

一个表三个sheet,testcase-产品1中的用例,src-产品1中的需求及需求id,dest-产品2中的需求和需求id
步骤:testcase产品1中的用例中关联产品1中的需求id,则testcase中的需求id从src中找到,
然后获取需求中文名再从dest中找到需求中文名获取需求id,回填到testcase中

3、代码

"""
需求:把产品1中的需求移到产品2中,产品1中的用例需要导入产品2,则对应的需求id发生变化,需要进行替换
一个表三个sheet,testcase-产品1中的用例,src-产品1中的需求及需求id,dest-产品2中的需求和需求id
步骤:testcase产品1中的用例中关联产品1中的需求id,则testcase中的需求id从src中找到,
然后获取需求中文名再从dest中找到需求中文名获取需求id,回填到testcase中
"""
import openpyxl
from openpyxl import load_workbook

# excel对象 -> sheet表单对象 -> cell单元格对象 -> 行和列、值属性
testexcel_wb = load_workbook("testcase_list.xlsx")
testexcel_ws = testexcel_wb['testcase']
# testcase表第八列为需求id,获取testcase中需求id
testexcel_cell = testexcel_ws.cell(1,8)
testexcel_value = testexcel_cell.value

# 创建其他两个sheet的表单对象
src_testexcel_ws = testexcel_wb['src']
dest_testexcel_ws = testexcel_wb['dest']

# testcase从第二行开始,获取需求id
# 这里踩了个坑,两边的表,需求id一个为数字,一个为字符串,导致没循环替换,所以最后都转化为字符串了
for row_testcase in range(2,testexcel_ws.max_row+1):
    testexcel_cell = testexcel_ws.cell(row_testcase, 8)
    testexcel_cell_value = str(testexcel_cell.value)

    # src表里循环,直到找到对应的需求id,src第二列为需求id
    for row_src in range (2,src_testexcel_ws.max_row+1):
        src_testexcel_cell = src_testexcel_ws.cell(row_src,2)
        src_testexcel_cell_value=str(src_testexcel_cell.value)
        # src找到需求id,并获取需求的中文名
        if testexcel_cell_value == src_testexcel_cell_value:
            src_name_value = src_testexcel_ws.cell(row_src,1).value
            print(src_name_value)
            # dest表中通过src的需求中文名找对应的需求id
            for row_dest in range (2,dest_testexcel_ws.max_row+1):
                dest_testexcel_cell = dest_testexcel_ws.cell(row_dest,1)
                if src_name_value== dest_testexcel_cell.value:
                    dest_id_value = dest_testexcel_ws.cell(row_dest,2).value
                    testexcel_cell.value = dest_id_value
                    info_message = f'需求:{src_name_value},id{src_testexcel_cell.value}替换为新id{dest_id_value}'
                    print(info_message)
                    break

testexcel_wb.save("testcase_list.xlsx")

 

posted @ 2021-11-04 11:23  依羽杉  阅读(50)  评论(0编辑  收藏  举报