1、只读方式打开excel文件
wb = openpyxl.load_workbook(filename=self._filename, read_only=True)
2、获取指定sheet
cfg_ws = wb['standard']
3、获取指定行的方法
获取sheet最大列,遍历指定行的单元格,若单元格为None则跳过,有数据则添加到指定列表
示例:
def read_excel_file(self):
# 只读方式打开excel文件
wb = openpyxl.load_workbook(filename=self._filename, read_only=True)
# 获取指定sheet
cfg_ws = wb['standard']
# 获取sheet指定行的方法
# 获取sheet最大列,遍历指定行的单元格,若单元格为None则跳过,有数据则添加到指定列表
cfg_ws_max_col = cfg_ws.max_column
iface_list = []
for i in range(1, cfg_ws_max_col + 1):
# 获取第4行,第N列数据
cell_value = cfg_ws.cell(row=4, column=i).value
if cell_value is None:
pass
elif "front" in cell_value or "rear" in cell_value or "back" in cell_value:
iface_list.append(cell_value)
else:
pass
for iface in iface_list:
print(iface)