打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[python]使用xlrd对Excel表格进行读写操作

一、安装xlrd模块

Python官网下载http://pypi.python.org/pypi/xlrd


二、使用介绍
2.1 导入模块

[python] view plain copy
  1. import xlrd  
2.2 打开Excel文件读取数据
[python] view plain copy
  1. data = xlrd.open_workbook("excelFile.xls")  
2.3 使用技巧
获取一个工作表
[python] view plain copy
  1. table = data.sheets()[0]             #通过索引顺序获取  
  2. table = data.sheet_by_index(0)       #通过索引顺序获取  
  3. table = data.sheet_by_name(u'Sheet') #通过名称获取  
获取整行和整列的值(数组)
[python] view plain copy
  1. table.row_values(i)  
  2. table.col_values(i)  
获取行数和列数
[python] view plain copy
  1. nrows = table.nrows  
  2. ncols = table.ncols  
循环行列表数据
[python] view plain copy
  1. for i in range(nrows):  
  2.     print table.row_values(i)  
单元格
[python] view plain copy
  1. cell_A1 = table.cell(0,0).value  
  2. cell_C4 = table.cell(2,3).value  
使用行列索引
[python] view plain copy
  1. cell_A1 = table.row(0)[0].value  
  2. 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 = '单元格的值'

[python] view plain copy
  1. table.put_cell(row,col,ctype,value,xf)  

例子1 通过索引索取值


[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlrd  
  4. def open_excel(file='file.xls'):  
  5.     try:  
  6.         data = xlrd.open_workbook(file)  
  7.         return data  
  8.     except Exception,e:  
  9.         print str(e)  
  10.   
  11. #根据索引获取Excel表格中的数据  
  12. #参数:file: Excel文件路径  
  13. #      colnameindex: 表头列名所在行的索引  
  14. #      by_index: 表的索引  
  15.   
  16. def excel_table_byindex(file='file.xls',colnameindex=0,by_index=0):  
  17.     data = open_excel(file)  
  18.     table = data.sheets()[by_index]  
  19.     nrows = table.nrows #行数  
  20.     ncols = table.ncols #列数  
  21.     colnames = table.row_values(colnameindex) #某一行数据  
  22.     list = []  
  23.     for rownum in range(1,nrows):  
  24.         row = table.row_values(rownum)#以列表格式输出  
  25.         if row:  
  26.             app = {}  
  27.             for i in range(len(colnames)):  
  28.                 app[colnames[i]] = row[i]  
  29.             list.append(app)#向列表中插入字典类型的数据  
  30.     return list  
  31.   
  32. def main():  
  33.     tables = excel_table_byindex(file='test.xls')  
  34.     for row in tables:  
  35.         print row  
  36.   
  37. if __name__=="__main__":  
  38.     main()  
运行结果为:


例子2 通过名字索引


[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlrd  
  4. def open_excel(file='file.xls'):  
  5.     try:  
  6.         data = xlrd.open_workbook(file)  
  7.         return data  
  8.     except Exception,e:  
  9.         print str(e)  
  10.   
  11. def excel_table_byname(file='file.xls',colnameindex=0,by_name=u'Sheet1'):  
  12.     data = open_excel(file)  
  13.     table = data.sheet_by_name(by_name)  
  14.     nrows = table.nrows #行数  
  15.     colnames = table.row_values(colnameindex) #某一行数据  
  16.     list = []  
  17.     for rownum in range(1,nrows):  
  18.         row = table.row_values(rownum)  
  19.         if row:  
  20.             app = {}  
  21.             for i in range(len(colnames)):  
  22.                 app[colnames[i]] = row[i]  
  23.             list.append(app)  
  24.     return list  
  25.   
  26. def main():  
  27.     tables = excel_table_byname(file='test.xls')  
  28.     for row in tables:  
  29.         print row  
  30.   
  31. if __name__=="__main__":  
  32.     main()  

例子3 通过xlwt写文件

[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlwt  
  4.   
  5. #新建一个excel文件  
  6. file = xlwt.Workbook()  
  7. #新建一个sheet  
  8. table = file.add_sheet('info',cell_overwrite_ok=True)  
  9. #写入数据table.write(行,列,value)  
  10. table.write(0,0,'wangpeng')  
  11. #保存文件  
  12. file.save('file.xls')  
得到的结果为



http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html

http://www.jb51.NET/article/42635.htm


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
接口测试基础——第5篇xlrd模块
python3读取excel文件只提取某些行某些列的值
【python】解析Excel中使用xlrd库、xlwt库操作,读取Excel文件详解(一)
Python Excel 读写及追加写入(xlrd、xlwt、openpyxl、XlsxWriter)
python操作Excel读写--使用xlrd
selenium实现excel文件数据的读、写
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服