此文章非本人

一、安装xlrd模块

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

二、使用介绍

1、导入模块

import xlrd

2、打开Excel文件读取数据

data = xlrd.open_workbook('excelFile.xls') 默认操作桌面上的excel

3、使用技巧

获取一个工作表

        table = data.sheets()[0]          #通过索引顺序获取
        table = data.sheet_by_index(0) #通过索引顺序获取
        table = data.sheet_by_name(u'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
 
        简单的写入
      row = 0
      col = 0
 
  # 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
  ctype = 1 value = '单元格的值'
     xf = 0 # 扩展的格式化
     table.put_cell(row, col, ctype, value, xf)
     table.cell(0,0)  #单元格的值'
     table.cell(0,0).value #单元格的值'
 
import xlrd
  
data =xlrd.open_workbook('test2.xls')
table = data.sheets()[0]
print table.nrows
print table.name for row_index in range(table.nrows):
for col_index in range(table.ncols):
print table.cell(row_index,col_index).value # 遍历所有的cell的值

三、Demo代码

Demo代码其实很简单,就是读取Excel数据。

例子:

   

# -*- coding: utf-8 -*-
import xdrlib ,sys
import xlrd
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)
#根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows #行数
ncols = table.ncols #列数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows): row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list #根据名称获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_name:Sheet1名称
def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nrows = table.nrows #行数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list def main():
tables = excel_table_byindex()
for row in tables:
print row tables = excel_table_byname()
for row in tables:
print row if __name__=="__main__":
main()

python使用xlrd 操作Excel读写的更多相关文章

  1. python使用xlrd操作Excel文件

    一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...

  2. python使用xlrd读取excel数据时,整数变小数的解决办法

    python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...

  3. MOOC(7)- case依赖、读取json配置文件进行多个接口请求-xlrd操作excel(11)

    xlrd操作excel # -*- coding: utf-8 -*- # @Time : 2020/2/12 9:14 # @File : do_excel_xlrd_11.py # @Author ...

  4. python操作Excel读写(使用xlrd和xlrt)

    包下载地址:https://pypi.python.org/pypi/xlrd   导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls ...

  5. python办公入门4:xlrd操作excel行

    操作excel行 1 #通过索引获取操作行 2 sheet=data.sheet_by_index(0) 3 #获取当前sheet下的有效行数 4 print(sheet.nrows) 5 #获取某一 ...

  6. python用openpyxl操作excel

    python操作excel方法 1)自身有Win32 COM操作office但讲不清楚,可能不支持夸平台,linux是否能用不清楚,其他有专业处理模块,如下 2)xlrd:(读excel)表,xlrd ...

  7. python通过openpyxl操作excel

    python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...

  8. 09 python学习笔记-操作excel(九)

    python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这几个模块可以使用pip安装, ...

  9. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

随机推荐

  1. Centos7修改系统时区timezone

    第一步:查询服务器时间 [root@localhost ~]# timedatectl Local time: Sat 2018-03-31 01:11:46 UTC Universal time: ...

  2. ip route rule 路由策略 高级路由 捆绑 网桥

    http://lwfs.net/2005/11/28/10/ #!/bin/bash IP0= IP1= GW0= GW1= NET0= NET1= DEV0=eth0 DEV1=eth1 # com ...

  3. python类型强转&二级容器

    Number 类型强转 int :         ---->>>   float,  bool,  complex,  str Float :        ---->> ...

  4. Hiveserver2高可用实现

    在生产环境中使用Hive,强烈建议使用HiveServer2来提供服务,好处有: 在应用端不用部署Hadoop和Hive客户端: 相比hive-cli方式,HiveServer2不用直接将HDFS和M ...

  5. Windows安装部署Tensorflow过程中的问题处理

    1 在配置Tensorflow过程中有一行命令是 python object_detection/builders/model_builder_test.py 过程中出现了 NameError: na ...

  6. linux中 shell编程 判断服务是否运行

    判断nginx是否运行中: if ps -ef|grep "nginx"|egrep -v grep >/dev/null then echo ok! else echo n ...

  7. Qt Xml Dom

    //Dom方式更改节点信息 bool XmlTools::SetValue(QString name,float exposure,float gain,float gamma,int brightn ...

  8. MYSQL存储过程中 使用变量 做表名

    ); DECLARE temp2 int; set temp1=m_tableName; set temp2=m_maxCount; set @sqlStr=CONCAT('select * from ...

  9. Android Studio设置自定义字体

    Android Studio设置自定义字体 (1)进入设置页面,File->Settings (2)自定义字体Editor->Colors&Fonts->Font (3)点击 ...

  10. IE兼容性问题的总结

    一.IE6/IE7对display:inline-block的支持欠缺 1.表现描述 这个应该算是很经典的ie兼容性问题了,inline-block作用就是将块级元素以行的等式显示.在主流浏览器中水平 ...