Python學習-Excel表格操作

一、安裝xlrd模塊
   到python官網下載http://pypi.python.org/pypi/xlrd模塊安裝,前提是已經安裝了python 環境。
   安裝命令: python setup.py install

二、使用介紹
  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

簡單例子:

# -*- coding: utf-8 -*- 
import xlrd
from pyExcelerator import *

#創建一個新的spreedsheet
def createExcel(excelName):
    workbook = Workbook()
    workbook.add_sheet('Sheet01')
    workbook.save(excelName)
    return workbook

if __name__ == '__main__':
    workbook = createExcel('test.xlsx')
    newSheet = workbook.add_sheet('Sheet02')
    #在單元格中插入數據
    newSheet.write(1, 1, 'tests')
    workbook.save('test.xlsx')
    
#    打開Excel文件讀取數據
    data = xlrd.open_workbook('test01.xlsx')
#    三種獲取工作表的方法
    table = data.sheets()[0]                #通過索引順序獲取
    table = data.sheet_by_index(0)          #通過索引順序獲取
    table = data.sheet_by_name(u'Sheet01')   #通過名稱獲取
#   獲取整行和整列的值(數組)
    print table.row_values(0)
    print table.col_values(0)
    
#    獲取行數和列數
    nrows=table.nrows
    ncols=table.ncols
    print nrows, ncols
       
#   循環行列表數據
    for i in range(nrows):
        print table.row_values(i)
 
#   獲取單元格的值方法一
    cell_A1 = table.cell(0,0).value
    cell_B2 = table.cell(1,1).value
    print cell_A1,cell_B2
 
#   獲取單元格的值方法二: 使用行列索引
    cell_A1 = table.row(0)[0].value
    cell_A2 = table.col(1)[0].value
    print cell_A1,cell_A2
    

兩個excel表格的例子:

import xlrd
from pyExcelerator import *

fname_base = "C:/android/tools/Mars2/eclipse/ws/TcAssignment/src/com/moto/www/Base.xlsx"
fname_rounds = "C:/android/tools/Mars2/eclipse/ws/TcAssignment/src/com/moto/www/Rounds.xlsx"

bk_base = xlrd.open_workbook(fname_base)
bk_rounds = xlrd.open_workbook(fname_rounds)

#shxrange = range(bk_base.nsheets)
try:
    sh_base = bk_base.sheet_by_name("Sheet1")
    sh_rounds = bk_rounds.sheet_by_name("Sheet1")
except:
    print "no sheet in %s named Sheet1" % fname_base
#get row number 
nrow_base = sh_base.nrows
nrow_rounds = sh_rounds.nrows
#get column number 
ncols_base = sh_base.ncols
ncols_rounds = sh_rounds.ncols
print "nrows %d, ncols %d" % (nrow_base, ncols_base)
print "nrows %d, ncols %d" % (nrow_rounds, ncols_rounds)
#get first row and first column data
#cell_value = sh.cell_value(0,1)
#print cell_value
  
row_list = []

w = Workbook()  #create a workbook
ws = w.add_sheet('TcAssigment')  #create a spreedsheet

for j in range(0, nrow_rounds):
#get data of each row
    tc_rounds = sh_rounds.cell_value(j, 0)
    print tc_rounds
    for i in range(0, nrow_base):
        base_tcs = sh_base.cell_value(i, 0)
        base_name = sh_base.cell_value(i, 1)
        print base_tcs == tc_rounds, '========'
        if base_tcs == tc_rounds:
            ws.write(j, 0, tc_rounds)
            ws.write(j, 1, base_name)
            break
        else:
            ws.write(j, 0, tc_rounds)

w.save('TcsAssignmentResult.xls')  #save


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章