需要先安装openpyxl库
通过pip命令安装: pip install openpyxl
源码如下:
- #!/usr/bin/python3
- #-*- coding:utf-8 -*-
- import openpyxl
- def getCell(wb, sheetname, column):
- #指定读取哪个Sheet(每个excel表格默认有三个Sheet:Sheet1,Sheet2,Sheet3)
- table = wb[sheetname]
- #读取哪一列数据
- cell = table[column]
- for c in cell:
- #过滤没有数据的行
- if (c.value):
- #打印结果
- print(c.value)
-
- if __name__ == "__main__":
- path = 'C:\\Users\\Desktop\\201808.xlsx'
- #excel对象
- wb = openpyxl.load_workbook(path)
- print(wb.sheetnames)
- print(wb.active)
- #传入表名,第一个Sheet的名称
- sheetname = wb.sheetnames[0]
- #传入列名,想读取哪一列就传入该列名
- column = 'B'
- getCell(wb, sheetname, column)
总结
以上所述是小编给大家介绍的python3 读取Excel表格中的数据,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!