Has its own Python-Excel module

Everyone knows that using pythonExcel Moduleopenpyxl can read Excel files.

Example

Suppose there is an Excel file as follows:
School.xlsx
Use openpyxl to read each< The contents of row 2 and column 2 of page a i=4>sheet. The code is as follows:

import openpyxl								#导入模块

excelfile = "school.xlsx"					# 要读取的 excel 文件
wb = openpyxl.load_workbook(excelfile)		# 生成excel文件对应的工作簿对象
ws = wb['老师']								# 切换到 老师 的 sheet 也,返回对应的sheet也对象
rvalue = ws.cell(2, 2).value				# 读取第2行第2列的数据
print(rvalue)								# 打印获取的数据

ws = wb['课程']
rvalue = ws.cell(2, 2).value
print(rvalue)

ws = wb['学生']
rvalue = ws.cell(2, 2).value
print(rvalue)

Output:
许口罩
Mathematics
小红

In the above process, although the desired data is obtained, the code looks very difficult to understand. If the amount of code is larger, not only will there be a lot ofmagic numbers, but when you come back and look at it after a while, you may have forgotten the code. mean.

This requires us to encapsulate these difficult-to-remember and difficult-to-read codes into easy-to-read methods, which is more conducive to development. And the methods in the class can be continuously improved as development needs arise. For example, when we want to switch sheet pages. You can name the method: switch_to_sheet("page name"); when we read data, we prefer to use Read the column name instead of the column number. For example, we want to read the Student page a>Name column, you can directly do thisget_value(2, "Name"), That is, read the contents of row 2, name column. This is consistent with the actual situation.
Of course, you can name the method with a meaningful and easy-to-read name according to your own ideas.

Custom packaging

The following is the encapsulated class, only the method name is given here,详细的类和测试代码见Attachment resources

class ExcelOpt:
    def __init__(self, file_name):   # 初始化
    def _build_header(self):   # 构建表头索引
    def _get_row_cell_value(self, row: str, col: str):  # 获取某行某列的值
    def switch_to_sheet(self, sheetname: str):  # 切换 sheet 页
    def get_value(self, row_num, col_name: str): # 获取某行某列的值
    def max_row(self):  # 获取当前 sheet 页最大行数
    def max_column(self): # 获取当前 sheet 页最大列数

With this class, reading a file is like the following code:

from excelopt import ExcelOpt

excelfile = "school.xlsx"
my_excel = ExcelOpt(excelfile)

# 读取老师页
my_excel.switch_to_sheet("老师")
for i in range(2, my_excel.max_row() + 1):
    print(my_excel.get_value(i, '姓名'))

# 读取课程页    
my_excel.switch_to_sheet("课程")
for i in range(2, my_excel.max_row() + 1):
    print(my_excel.get_value(i, '上课地点'))

Output:
Xu Mask
Zheng Lunchbox
Yao Waihai
Guo Jiajia
Zhou Shini
Classroom 202, Science Building
Classroom 203, Qinxue Building
Room 204, Inspirational Building Classroom
Classroom 205, Xinming Building
Classroom 206, Xueshang Building

Attachment resourcesThe file only has a few reading methods. Of course, you can also encapsulate the code for writing Excel into the easy-to-read method you want. Everyone can play to their heart’s content.

Guess you like

Origin blog.csdn.net/weixin_44131612/article/details/131062794