python使用xlrd 操作Excel读写

此文章非本人

一、安装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()
时间: 2024-08-08 07:54:44

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

MFC 操作excel 读写 感悟

在项目实现中,要操作excel 对表格进行读写,或者是将listcontrol控件里面的内容导入到表格中,为此在网上找了很多代码作为参考,但是都没有达到自己想要的效果! 在此,将自己在项目开发中遇到的问题和解决方法一一列出,希望能帮到其他人.    问题1   出现range等类的重定义,报错100多项 出错原因 主要是对于excel类库的多次添加导致,可以将类库删除后,重新添加.注意:在添加类中,应该添加自己需要的类,最好不要将类全部添加,否则程序运行会很慢. 同时,对于excel类库中类的操

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: Hero Liu import xlrd # 打开工作簿 work_book = xlrd.open_workbook("../data/test_data.xlsx") # 通过指定索引获取sheet表,索引从0开始:也可以通过指定表名来获取 # table = work_book.

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

包下载地址:https://pypi.python.org/pypi/xlrd 导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls') #注意这里的workbook首字母是小写 查看文件中包含sheet的名称 data.sheet_names() 得到第一个工作表,或者通过索引顺序 或 工作表名称 table = data.sheets()[0] table = data.sheet_by_index(0) table = dat

Python学习笔记-操作excel

python操作excel:使用pip安装即可 一.xlwt:写excel import xlwt book = xlwt.Workbook() #新建一个excel sheet = book.add_sheet('sheet1') #加sheet页 sheet.write(0,0,'姓名') #行.列.写入的内容 sheet.write(0,1,'年龄') sheet.write(0,2,'性别') book.save('stu.xls') #结尾一定要用.xls import xlwt ti

python通过openpyxl操作excel

python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作,也就是对后缀为xlsx进行操作. Excel 主要有三大元素,工作簿,Sheet 页,单元格,一个工作簿可以包含多个Sheet页面,而Sheet页由N多个单元格组成,而单元格主要用来存储数据: 一.安装插件 pip install openpyxl 二.创建Excel文件 操作excel之前,首先

python 之 xlrd模块 excel的读使用

import xlrd data = xlrd.open_workbook("databases.xls").sheet_by_name(u"mall") nrows = data.nrows ncols = data.ncols c = {} def data_mall(): for i in range(nrows): print(data.row_values(i)) # a={i:data.row_values(i)} # c.update(a) # pri

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

现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法长时间做某种重复性的枯燥操作.想象这样一个场景,我们有个几千行的表要填,需要根据姓名输入其对应的身份证号,但之前我们已经做过一个类似的表,同样的一些人的姓名跟身份证号是完整的,那么我们就需要通过一个个查找姓名,然后把身份证号码复制到我们当前要做的表里去. 当我日复一日重复着这些操作的时候,我都很想有

python操作Excel读写--使用xlrd(转)

一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') 3.使用技巧 获取一个工作表 table = data.sheets()[0]          #通过索引顺序获取 table = data.sheet_by_index(

Python实现操作Excel读写(使用xlrd模块实现)

一.安装xlrd模块 安装前提是已经安装了python 环境: 1.到python官网下载http://pypi.python.org/pypi/xlrd 下载压缩包 2.解压所下载的压缩包 3.CD到解压目录C:\users\lyj>cd /d G:\Python34\Lib\xlrd-1.0.0,执行 python setup.py install 二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('exc