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、使用技巧
获取一个工作表
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读写的更多相关文章
- python使用xlrd操作Excel文件
一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...
- python使用xlrd读取excel数据时,整数变小数的解决办法
python使用xlrd读取excel数据时,整数变小数: 解决方法: 1.有个比较简单的就是在数字和日期的单元格内容前加上一个英文的逗号即可.如果数据比较多,也可以批量加英文逗号的前缀(网上都有方法 ...
- 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 ...
- python操作Excel读写(使用xlrd和xlrt)
包下载地址:https://pypi.python.org/pypi/xlrd 导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls ...
- python办公入门4:xlrd操作excel行
操作excel行 1 #通过索引获取操作行 2 sheet=data.sheet_by_index(0) 3 #获取当前sheet下的有效行数 4 print(sheet.nrows) 5 #获取某一 ...
- python用openpyxl操作excel
python操作excel方法 1)自身有Win32 COM操作office但讲不清楚,可能不支持夸平台,linux是否能用不清楚,其他有专业处理模块,如下 2)xlrd:(读excel)表,xlrd ...
- python通过openpyxl操作excel
python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...
- 09 python学习笔记-操作excel(九)
python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这几个模块可以使用pip安装, ...
- 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中
现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...
随机推荐
- css学习(1)
一.标签选择器 标签{属性:值;} <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- docker设置容器固定ip
docker安装后,默认会创建三种网络类型,bridge.host和none,可通过如下命令查看 sudo docker network ls 1 bridge:网络桥接 默认情况下启动.创建容器都是 ...
- echarts3更新
dataRange: { realtime: false, itemHeight: 80, splitNumber:6, borderWidth:1, textStyle: { color: '#33 ...
- (转) C#之VS自带RDLC报表学习
原文地址:http://blog.csdn.net/hk_5788/article/details/49846905 原文工具VS2010,测试版本工具VS2013 报表是这样设计的: 看看结果: ...
- ERROR: iterator not incrementable || iterator not decrementable
这个错误提示:迭代器不可以增加 exmaple: vector<int> tVecInt; vector<int>::reverse_iterator iterInt = tV ...
- JSP 静态文件路径配置
在JSP中,往往需要引入一些静态文件. 例如这样引用. 往往因为目录结构的问题,不知道是用点还是 .../ ./ ../表示相对当前路径的上一级目录:./表示相对当前的路径: 这里有个快捷的办法. l ...
- 按键显示按键编码 keycode
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 13.纯 CSS 创作一个冒着热气的咖啡杯
原文地址:https://segmentfault.com/a/1190000014734039 感想:伪元素::before ::after 真的很强大,动画也是. HTML代码: <div ...
- hdfs dfsadmin 命令详解
hdfs dfsadmin [-report [-live] [-dead] [-decommissioning]] [-safemode <enter | leave | get | wait ...
- hive grouping sets 实现原理
先下结论: 看了hive 1.1.0 grouping sets 实现(从源码及执行计划都可以看出与kylin实现不一样),(前提是可累加,如sum函数)他并没有像kylin一样先按照group by ...