[Python Excel read Excel]

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43342981/article/details/102769426

python read Excel data

1, you need to install xlrd and xlwt

python Excel data read and XLRT xlrd need to use two libraries, xlrd for reading Excel, xlrt for writing excel.
Installation with pip follows:
pip the install to xlrd
pip the install xlwt

2, and the introduction information acquiring excel

Comprising the following code:
1, read excel
2, excel obtain the names of all of the sheet
3, the number of read rows of a sheet wherein the number of columns
4, the sheet is read a row, a column, a cell data
5 to view a row or a column of data format

import xlrd
import xlwt
file = 'py-test-1026-cd.xlsx'
wb = xlrd.open_workbook(filename = file) #读取excel文件 
print(wb.sheet_names()) #查看所有sheet的名字
sheet_zz = wb.sheet_by_index(1) #获取花名册sheet的数据,通过索引获得
print(sheet_zz)
sheet_zz = wb.sheet_by_name("花名册") #通过名字获得
print(sheet_zz)
sheet_zz_rows = sheet_zz.row_values(0) #获取第一行,即行标题
print(sheet_zz_rows)
sheet_zz_cols  = sheet_zz.col_values(0) #获取第一列
print(sheet_zz_cols)
sheet_zz_cell = sheet_zz.cell(0,0) #获取第一行第一列的单元格
rows = sheet_zz.nrows #获取行数
cols = sheet_zz.ncols #获取列数
print("行数为{},\n列数为{}".format(rows,cols))
print("第6列的格式是{}".format(sheet_zz.cell(1,5).ctype)) #第5列应该是日期格式,查看第五列是否是日期格式

Guess you like

Origin blog.csdn.net/weixin_43342981/article/details/102769426