一、安装xlrd模块

有多种方法:

1、cmd命令行输入 pip install xlrd

2、到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。

3、pycharm添加模块

二、使用介绍

  1、导入模块

      import xlrd

   2、打开Excel文件读取数据

       data = xlrd.open_workbook('excelFile.xls')

   3、使用技巧

        获取一个工作表

 

        table = data.sheets()[0]          #通过索引顺序获取
 
        table = data.sheet_by_index(0) #通过索引顺序获取

 

        table = data.sheet_by_name('Sheet1')#通过名称获取
 
        获取整行和整列的值(数组)
   
         table.row_values(i)
 
         table.col_values(i)
 
        获取行数和列数
  
        nrows = table.nrows
 
        ncols = table.ncols
       
        循环行列表数据
        for i in range(nrows ):
      print table.row_values(i)
 
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
 
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value
 
 
posted on 2017-10-24 10:28  奈何鸿飞雁  阅读(185)  评论(0)    收藏  举报