Python 读取Excel文件

#! usr/bin/env python
import xlrd
import time

class readExcel:
    def __init__(self,excel_file):
        self.excel_file = excel_file
        self.http_method = ''
        self.request_url = ''
        self.request_param = ''

    def read_excel(self):
        excel_data = []
        #打开Excel文件
        data = xlrd.open_workbook(self.excel_file)

        #选择一个工作表进行读取
        table = data.sheet_by_index(0)

        for i in range(1,table.nrows):
            excel_data.clear()
            if table.cell(i,7).value.replace('\n','').replace('\r','') != 'Yes':
                continue
            #读取工作表中每一行的数据(table.row_value()返回的是列表形式)
            #row_data = table.row_values(i)
            self.case_id = str(int(table.cell(i, 0).value)).replace('\n','').replace('\r','')
            self.test_desc = table.cell(i, 1).value.replace('\n','').replace('\r','')
            self.request_url = table.cell(i, 3).value.replace('\n','').replace('\r','')
            self.test_method = table.cell(i, 4).value.replace('\n','').replace('\r','')
            self.request_param = table.cell(i, 5).value.replace('\n','').replace('\r','')
            self.check_point = table.cell(i, 6).value.replace('\n','').replace('\r','')
            self.active = table.cell(i, 7).value.replace('\n','').replace('\r','')
            excel_data.append((self.case_id,self.test_desc,self.request_url,self.test_method,self.request_param,self.check_point,self.active))
            yield excel_data
            print('我执行了一次')


if __name__ == '__main__':
    file = readExcel('TestCase.xlsx')
    for i in file.read_excel():
        print(i)

 

posted @ 2018-02-26 16:31  我是旺旺  阅读(418)  评论(0编辑  收藏  举报