码迷,mamicode.com
首页 > 编程语言 > 详细

python28 excel读取模块xlrd

时间:2019-02-02 12:52:09      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:名称   info   import   xlsx   xls   dex   pre   nbsp   png   

安装:

pip install xlrd

技术图片

 

简单使用:

import xlrd

book = xlrd.open_workbook(rC:\Users\dinghanhua\Desktop\yqqapi.xlsx) # 打开excel
print("the number of sheets:",book.nsheets) # sheet数量
print("sheet_names:",book.sheet_names()) # sheetname列表

sheet1 = book.sheet_by_index(0) # 通过索引取sheet
print(sheet1.name,sheet1.nrows,sheet1.ncols) #sheet名称、行数、列数
print(sheet1.cell(0,0).value) #cell值
print(sheet1.cell_value(0,1)) #cell值

sheet2 = book.sheet_by_name("sheet2") # 通过sheetname取sheet
print(sheet2.name,sheet2.nrows,sheet2.ncols)

# 获取sheet所有的数据
for row in range(sheet1.nrows):
    for col in range(sheet1.ncols):
        print(sheet1.cell_value(row,col),end=\t)
    print(\n)

print(sheet1.col_values(0,1,sheet1.nrows)) # 获取第一列,第2行的所有值

print(sheet1.row(1)) # 获取第二行的值

for col in range(sheet1.ncols): # 按列获取值,每列是list
    print(sheet1.col_values(col,0,sheet1.nrows))

for row in range(sheet1.nrows): # 按行获取值;每行都是list
    print(sheet1.row_values(row,0,sheet1.ncols))

 

 

the end!

 

python28 excel读取模块xlrd

标签:名称   info   import   xlsx   xls   dex   pre   nbsp   png   

原文地址:https://www.cnblogs.com/dinghanhua/p/10348076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!