1.pip安装xlrd和xlwt
pip install xlrd
pip install xlwt
2.读取excel
read.xls表格中内容如下所示:
read.py源代码如下:
# -*- coding: utf-8 -*-
import xlrd
import os
class ExcelReade(object):
def __init__(self, excel_name, sheet_name):
"""
# 我把excel放在工程包的data文件夹中:
# 1.需要先获取到工程文件的地址
# 2.再找到excel的文件地址(比写死的绝对路径灵活)
os.path.relpath(__file__)
1.根据系统获取绝对路径
2.会根据电脑系统自动匹配路径:mac路径用/,windows路径用\
3.直接使用__file__方法是不会自动适配环境的
"""
# 获取当前.py文件所在文件夹层
dir_path = os.path.dirname(os.path.realpath(__file__))
# 获取excel所在文件目录
self.excel_path = os.path.join(dir_path, "", excel_name)
# 打开指定的excel文件
self.date = xlrd.open_workb