python officeの操作

1038 ワード

xlrdはExcelの読み取り操作のライブラリです.
xlwtはExcelへの書き込み操作のライブラリです.
import xlrd  
import xlwt  
from datetime import date,datetime  
  
def read_excel():  
  #       
  workbook = xlrd.open_workbook(r'F:\demo.xlsx')  
  #     sheet  
  print workbook.sheet_names() # [u'sheet1', u'sheet2']  
  sheet2_name = workbook.sheet_names()[1]  
  
  #   sheet        sheet    
  sheet2 = workbook.sheet_by_index(1) # sheet   0    
  sheet2 = workbook.sheet_by_name('sheet2')  
  
  # sheet   ,  ,    
  print sheet2.name,sheet2.nrows,sheet2.ncols  
  
  #          (  )  
  rows = sheet2.row_values(3) #          
  cols = sheet2.col_values(2) #          
  print rows  
  print cols  
  
  #          
  print sheet2.cell(1,0).value.encode('utf-8')  
  print sheet2.cell_value(1,0).encode('utf-8')  
  print sheet2.row(1)[0].value.encode('utf-8')  
    
  #               
  print sheet2.cell(1,0).ctype  
  
if __name__ == '__main__':  
  read_excel()