python3操作excel:
先安装第三方模块xlrd、xlwt
C:\Users\xxx>pip install xlrd
Collecting xlrd
Downloading https://files.pythonhosted.org/packages/07/e6/e95c4eec6221bfd8528bcc4ea252a850bffcc4be88ebc367e23a1a84b0bb/xlrd-1.1.0-py2.py3-none-any.whl (108kB)
100% |████████████████████████████████| 112kB 32kB/s
Installing collected packages: xlrd
Successfully installed xlrd-1.1.0
C:\Users\xxx>pip install xlwt
Collecting xlwt
Downloading https://files.pythonhosted.org/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB)
100% |████████████████████████████████| 102kB 259kB/s
Installing collected packages: xlwt
Successfully installed xlwt-1.3.0
- 读取sheet名字
#导入xlrd
import xlrd
#打开工作薄
workbook=xlrd.open_workbook('excel01.xls')
#输出所有工作表名字
print(workbook.sheet_names())
#获得工作表
sheet01=workbook.sheets()[0]
#获得第一行值
values=sheet01.row_values(0)
#输出第一行值
print(values)
#获取行数
nrows=sheet01.nrows
#遍历工作表中所有内容
for i in range(nrows):
if i==0:#如果第一行为空则跳过
continue
print(sheet01.row_values(i))
输出
===== RESTART: D:\Python\test\课时8:Python面向对象编程\课时8:Python面向对象编程-课后作业.py =====
['Sheet1', 'Sheet2', 'Sheet3']
['', '', '', '', '']
['姓名', '年龄', '出生日期', '爱好', '关系']
['xiaoming1', 17.0, 42129.0, 'basketball', 'friend']
['zhangsan1', 18.0, 41796.0, 'football', '']
['', '', '', '', '']
['lisi1', 19.0, 41462.0, 'basketball', 'friend']
['wangwu1', 20.0, 40941.0, 'readb book', '']
['zhaoliu1', 'None', '', '', '']
>>>