读excel文件,借助openpyxl工具

读excel文件,借助openpyxl工具

import os

import requests

os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

base_url = "https://testnet.starscan.io/explorer-api"
# https://starscan.io/explorer-api/address/queryAddressDetails

# res = requests.post(url=base_url,json={"address": "fxvaloper1tc0v3lkxjwth92pu0dprhnzzetqcnn8yk9q0xx", "chainName": "fxcore"})
# print(res)
import os

import openpyxl

file_dir = os.path.abspath(os.path.dirname(__file__))

class GetDatas:

    def getExcels(self,sheet):
        data_list = []
        # 获取工作簿D:\works\exports.xlsx
        wk = openpyxl.load_workbook(f"{file_dir}\exports.xlsx")
        # 获取工作表
        sheet1 = wk[sheet]
        for i in range(2, sheet1.max_row + 1):
            dict_data = {}
            for j in range(1, sheet1.max_column + 1):
                key = sheet1.cell(1, j).value
                value = sheet1.cell(row=i, column=j).value
                dict_data[key] = value
            data_list.append(dict_data)
        return data_list


if __name__ == '__main__':
    datas = GetDatas().getExcels("Sheet1")
    print(datas[0]["url"])
    print(datas[0]["headers"])
import json
import unittest

import requests
from ddt import ddt, data
from read_excel.common import base_url
from read_excel.get_excel_datas import GetDatas


@ddt
class TestGetData(unittest.TestCase):
    def setUp(self) -> None:
        self.url = base_url
    #
    # @data(*GetDatas().getExcels("Sheet2"))
    # def test_datas(self, dataList):
    #     print(dataList)
    #     res = requests.post(url=self.url, json=dataList)
    #     print(res.json())

    @data(*GetDatas().getExcels("Sheet1"))
    def test_url_datas(self, dataList):
        print(dataList)
        headers = json.loads(dataList["headers"])
        datas = json.loads(dataList["datas"])
        # print(headers)
        # print(datas)
        response = requests.request(method=dataList["method"], url=self.url+dataList["url"], headers=headers, json=datas)
        # print(response)
        assert response.status_code == dataList["status_code"]
        res = response.json()
        print(res)
        assert res["msg"] == dataList["context_actual"]


if __name__ == '__main__':
    unittest.main()

你可能感兴趣的:(excel,python)