python3 读取Excel表格

需要先安装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)

猜你喜欢

转载自blog.csdn.net/qq_34500270/article/details/83089588
今日推荐