Python uses openpyxl operation excel spreadsheet

A, openpyxl use

pip install openpyxl

Step one: Open the workbook (read Excel files saved all of the data for the Workbook object)

workbook = openpyxl.load_workbook('cases.xlsx')

Step two: Select the form object

sheet = workbook['login']

The third step: read data through a form selected table

① read the contents (fifth row fourth column)

data = sheet.cell(row=5,column=4)
printa(data.value) # 获取内容用value方法

② written content (save to take effect)

sheet.cell(row=7,column=3,value='("Python","123456")')
# 写入内容后,一定要保存才会生效
workbook.save('cases.xlsx')

③ obtain the maximum rows and maximum columns

# 最大行
sheet.max_row
# 最大列
sheet.max_column

④ Get all lattice objects in rows, each row of the grid into a tuple

sheet.rows

* Precautions: Do not knock box in the table

Two, openpyxl package

Guide package

import openpyxl

Package

class ReadExcel(object):
    """操作Excl文件"""

    def __init__(self, fileName, sheetName):
        """
        初始化方法
        :param fileName: Excel文件名
        :param sheetName: 表单名
        """
        self.fileName = fileName
        self.sheetName = sheetName

    def open(self):
        """打开工作簿,选中表单"""
        self.wb = openpyxl.load_workbook(self.fileName)
        self.sh = self.wb[self.sheetName]

    def save(self):
        """保存工作簿对象的方法"""
        self.wb.save(self.fileName)
        self.wb.close()  # 这一行加不加关系不大,加了可以释放内存

1, data is read

① The data is stored as a dictionary for each

    def read_data_dict(self):
        """读取数据(将每条数据存储成字典类型)"""
        # 打开工作簿
        self.open()
        # 通过rows获取Excel文件中所有的行数据,然后把数据转成列表
        rows = list(self.sh.rows)
        # 表头
        title = []
        # 遍历Excel文件中的第一行表头信息
        for i in rows[0]:
            title.append(i.value)
        # 用例数据列表
        cases = []
        # 遍历用例数据行
        for row in rows[1:]:
            # 定义一个列表存放每一行的数据
            data = []
            # 遍历每一行的数据
            for r in row:
                data.append(r.value)
            # 把每一行的数据通过zip进行打包,然后转成字典,存入到用例数据列表中
            case = dict(zip(title, data))
            cases.append(case)
        return cases

② The data is stored for each type of object to CaseData

class CaseData(object):
    """保存用例数据类"""
    pass
    def read_data_obj(self):
        """读取数据(将数据保存到CaseData类中)"""
        self.open()
        rows = list(self.sh.rows)
        # 表头
        title = []
        # 遍历Excel文件中的第一行表头信息
        for i in rows[0]:
            title.append(i.value)
        # 定义用例列表,用来存放用例类列表
        cases = []
        # 遍历用例数据行
        for row in rows[1:]:
            # 定义一个列表存放每一行的数据
            data = []
            # 遍历每一行的数据
            for r in row:
                data.append(r.value)
            # 把每一行的数据通过zip进行打包,然后转成字典,存入到用例数据列表中
            case = dict(zip(title, data))
            # 定义一个用例存放类对象
            case_obj = CaseData()
            for k, v in case.items():
                # 通过setattr()给对象添加属性
                setattr(case_obj, k, v)
            # 把对象添加到列表中
            cases.append(case_obj)
        return cases

2, write data

    def write_data(self, row, column, value):
        """
        写入数据
        :param row: 行
        :param column: 列
        :param value: 数据
        """
        self.open()
        # 指定行列进行写入数据
        self.sh.cell(row=row, column=column, value=value)
        # 保存
        self.wb.save(self.fileName)
        # 关闭
        self.wb.close()

** need to use excel when the operation can be called directly, reducing the amount of repetitive code.

Guess you like

Origin www.cnblogs.com/desireyang/p/12059916.html