Python interface automatically encapsulates exporting excel methods and reading and writing excel data

1. First of all, we need to think about how to export excel on the page and write it to the file using python.

Before encapsulation, you need to confirm what kind of data type is returned by the python export excel interface,
as follows: Let's first look at the return result without processing it. The directly received data type is an object, and the return value cannot be obtained.

 At this point we need to process the returned data, as follows;
response.text # Response text data (string)

 Change the returned data type to dict, response.json()** This will make it easier for us to get the data according to the dictionary operation**

But our current operation is to obtain the data of the exported file. The exported excel is a binary file:

response.content # Response content returned (binary)

Next, we use the response.content method to write this binary file into excel:

2. The following packaging:

class Export:
    """
    导出域
    """
    def __init__(self, token):
        self.token = token
        self.headers = {
                'Authorization': self.token,
                'Content-Type': 'application/json;charset=UTF-8'
        }
        
        ```
def export_sku_excel(self, payload, path):
    """
            商品:商品明细导出
            """
    url = f'{HOST}/api/v1/commodity/exportSKU'
    res = client.post(url=url, json=payload, verify=False, headers=self.headers)
    resp = res.content
    with open(path, 'wb') as f:  # 第一个参数是保存文件路径,不加路径就是当前路径
        if res.status_code == 200:
            return f.write(resp)
        else:
            return False
 

As above, first receive the binary file, and then use the operation excel method 'wb' to write the binary file

After writing the file above, we need to read the file data to make assertions during the test process , as follows:

class ExcelMethod:
    def __init__(self, filename):
        self.filename = filename
 
    def read_excel(self, row, col):
        """
        读取导出文件的数据
        Returns:excel单元格数据
        """
        wb = xlrd.open_workbook(self.filename)
        sheet_name = wb.sheet_names()[0]
        sheet1 = wb.sheet_by_index(0)
        cellInfo = sheet1.cell_value(row, col)  # 获取文件中某单元格的值
        return cellInfo  # 返回文件单元格数据

The above is an encapsulation method for writing, reading and exporting excel.
It is worth noting that I use the Python built-in library xlrd to read and write excel files. The xls format file xlrd can read and write, and xlrd uses version 1.7, the latest version. Xls files are not supported. The openpyxl library only supports xlsx format files.

 There is also a way to read Excel files using the pandas library

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you! 

Guess you like

Origin blog.csdn.net/2301_78276982/article/details/132715705