【python】 xlrd的使用

来源:互联网 发布:域名批量生成查询 编辑:程序博客网 时间:2024/05/20 14:39
详细代码: 

点击(此处)折叠或打开

  1. Demo:
  2. 1. open a xls file
  3. 2. read a sheet
  4. 3. read a cell or row or col
  5. 4. modify the cell
  6. '''

  7. import xlrd
  8. import os


  9. class XlsEngine():
  10.     """
  11.     The XlsEngine is a demo class for excel openration
  12.     Just for some basic test or the using or the 3rd class in python
  13.     """
  14.     def __init__(self,__name):
  15.         # define class variable
  16.         self.xls_name = __name
  17.         self.xlrd_object = None
  18.         self.isopenfailed = True
  19.     def open(self):
  20.         try:
  21.             self.xlrd_object = xlrd.open_workbook(self.xls_name)
  22.             self.isopenfailed = False
  23.             pass
  24.         except :
  25.             self.isopenfailed = True
  26.             self.xlrd_object = None
  27.             print("open %s failed \n"%self.xls_name)
  28.             pass
  29.         finally:
  30.             '''
  31.             do nothing
  32.             '''
  33.             pass
  34.         return [self.isopenfailed,self.xlrd_object]
  35.     
  36.     def dump_sheet(self):
  37.         if self.isopenfailed == False:
  38.             '''
  39.             dump the sheet
  40.             
  41.             usging for getting the sheet
  42.             table = data.sheets()[0]
  43.             table = data.sheet_by_index(0)
  44.             table = data.sheet_by_name(u'Sheet1')
  45.             '''
  46.             for name in self.xlrd_object.sheet_names():
  47.                 table = self.xlrd_object.sheet_by_name(name)
  48.                 print("sheet %s rownums=%d colnums=%d"%(name,table.nrows,\
  49.                                                          table.ncols))
  50.         else:
  51.             print("file %s is not open \n"%self.xls_name)
  52.             
  53.     def dump_cell(self,sheet_index,cell_row,cell_col):
  54.         try:
  55.             table=self.xlrd_object.sheet_by_index(0)
  56.             value=table.cell(cell_row,cell_col).value
  57.             print("value=%d"%value)
  58.             pass
  59.         except:
  60.             pass
  61.      
  62.     
  63.     def modify_cell(self,sheet_index,cell_row,cell_col,__value):
  64.         try:
  65.             table=self.xlrd_object.sheet_by_index(0)
  66.             value=table.cell(cell_row,cell_col).value
  67.             print("value=%d"%value)
  68.             table.put_cell(cell_row,cell_col,1,__value,0)
  69.             value=table.cell(cell_row,cell_col).value
  70.             print("value=%d"%value)
  71.             pass
  72.         except:
  73.             print("error")
  74.             
  75.         pass
  76.             
  77.             




  78. if __name__ == '__main__':
  79.     tt=XlsEngine('D:\\tt.xls

//// 以下为参考博文:
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(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 #单元格的值'
 

 

阅读(894) | 评论(0) | 转发(0) |
0

上一篇:【python】 读取Excel文件并绘制图表

下一篇:FreeSWITCH 1.2.5.3 Step by Step Install

相关热门文章
  • python 自动化测试平台 Robot ...
  • python snmp 自动化2-在python...
  • python snmp 自动化测试1-安装...
  • 自动化测试详细测试计划 模板...
  • python snmp 自动化3-修改pyth...
  • Python 动态创建类
  • 一个基于multiprocessing的并...
  • Python 数据库管理与操作...
  • 利用Bicho抓取基于Jira的缺陷...
  • Lua中的协程即协同程序...
给主人留下些什么吧!~~
原创粉丝点击