python处理excel之读:xlrd模块

# -*- coding:utf-8 -*-
import xlrd

path = r'D:/工作簿1(已自动还原).xlsx'

# 打开excel文件读取数据
data = xlrd.open_workbook(path)
# 获取excel中所有工作表sheets
sheets = data.sheets()
# 获取所有sheet的名称集合
sheet_names = data.sheet_names()

# 获取sheet对象的三种方式
# 1
for i in range(len(sheets)):
    sheet = sheets[i]
    print(sheet)
# 2
for i in range(len(sheets)):
    sheet = data.sheet_by_index(i)
    print(sheet)
# 3
for i in range(len(sheet_names)):
    sheet = data.sheet_by_name(sheet_names[i])
    print(sheet)


# 行的操作
for i in range(len(sheets)):
    sheet = sheets[i]
    rows = sheet.nrows
    cols = sheet.ncols
    for j in range(rows):
        print(sheet.row_values(j))
        for k in range(cols):
            value = sheet.row_values(j)[k]
            if value:       # 去除空值
                print(value)

# 列的操作
for i in range(len(sheets)):
    sheet = sheets[i]
    rows = sheet.nrows
    cols = sheet.ncols
    for j in range(cols):
        print(sheet.col_values(j))
        print(sheet.col_types(j))
        for k in range(rows):
            value = sheet.col_values(j)[k]
            if value:
                print(value)

# 单元格的操作
for i in range(len(sheets)):
    sheet = sheets[i]
    rows = sheet.nrows
    cols = sheet.ncols
    for j in range(rows):
        for k in range(cols):
            value = sheet.cell_value(j, k)
            if value:
                print(value)

 

posted on 2019-02-19 22:45  0x153_小波  阅读(180)  评论(0编辑  收藏  举报