Bootstrap

python 操作Excel(读取)

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:victor Li
import openpyxl

from common.dir_config import DirInfo
from common.elements import Element


class OpearteExcel:
    file_name = ""

    def __init__(self, file_name):
        self.file_name = file_name

    def get_book(self):
        wb = openpyxl.load_workbook(DirInfo.file_path.value + "test.xlsx")
        return wb

    def read_case(self):
        wb = self.get_book()
        ws = wb["测试用例集合"]
        case_list = []
        # 从第二行到最大行  并只遍历第二列
        for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=2, max_col=2):
            cell = [cell.value for cell in row]
            if cell[0] is None:
                break
            case_list.append(cell[0])
        return case_list
    def read_element(self):
        wb = self.get_book()
        ws = wb['测试元素库']
        for row in ws.iter_rows(min_row=2,max_row=ws.max_row,min_col=1):
            cell=[cell.value for  cell in row]
            if cell[0] is None:
                break
            Element.update({cell[0]:[cell[1],cell[2]]})
        print(Element)
   # def
if __name__ == '__main__':
    OpearteExcel("test.xlsx").read_element()

;