1. 程式人生 > >Python excel讀寫

Python excel讀寫

pty xxx oam 切換 ont ext ces cell 創建

  1 # coding=utf-8
  2 
  3 print "----------------分割線 xlrd--------------------"
  4 import xlrd
  5 #打開一個wordbook
  6 book = xlrd.open_workbook("excel_1.xls")
  7 
  8 worksheets = book.sheet_names() #uoqu所有sheet名稱
  9 # print ‘workshets:‘,worksheets
 10 # sheet = book.sheets() #獲得全部sheet
 11 # for item in sheet:
12 # print item.name 13 14 sheet = book.sheet_by_index(0) #通過下標切換sheet 15 # sheet = book.sheet_by_name(‘wsf‘) #通過sheet的名稱切換 16 17 rows = sheet.nrows #行數 18 cols = sheet.ncols #列數 19 20 r_data = sheet.row(1) #獲取指定行數據,返回list 21 # print r_data #[text:u‘LAD‘, text:u‘V100R001C01B001‘, text:u‘SDFA V100R001C01B001‘, empty:u‘‘]
22 c_data = sheet.col(0) #獲取指定列數據,返回list 23 # print c_data #[text:u‘product name‘, text:u‘LAD‘, text:u‘DSF‘, text:u‘ASD‘, text:u‘EFSW‘] 24 ce_data = sheet.cell(1,1).value #獲取指定單元格的數據 25 # print ce_data 26 27 #獲取sheet中的所有行數據 28 for row in xrange(rows): 29 r_data = sheet.row_values(row) 30
print r_data 31 #獲取sheet中的所有列數據 32 for col in xrange(cols): 33 c_data = sheet.col_values(col) 34 print c_data 35 #獲取sheet中所有單元格的數據 36 for row in xrange(rows): 37 for col in xrange(cols): 38 ce_data = sheet.cell_value(row, col) 39 print "cell:",ce_data 40 41 print "----------------分割線 xlwt--------------------" 42 43 import xlwt 44 ‘‘‘xlwt不能操作已存在的excel,新建excel寫入數據‘‘‘ 45 #創建workbook對象 46 workbook = xlwt.Workbook() 47 #創建sheet對象,新建sheet 48 sheet1 = workbook.add_sheet(xlwt, cell_overwrite_ok=True) 49 sheet2 = workbook.add_sheet(xled, cell_overwrite_ok=True) 50 51 #---設置excel樣式--- 52 #初始化樣式 53 style = xlwt.XFStyle() 54 #創建字體樣式 55 font = xlwt.Font() 56 font.name = Times New Roman 57 font.bold = True #加粗 58 #設置字體 59 style.font = font 60 #使用樣式寫入數據 61 # sheet.write(0, 1, "xxxxx", style) 62 63 #向sheet中寫入數據 64 sheet1.write(0, 0, nihao xlwt, style) 65 sheet1.write(0, 1, nimei) 66 sheet2.write(0, 0, nihao xlrd, style) 67 sheet2.write(0, 1, nimei) 68 #保存excel文件,有同名的直接覆蓋 69 workbook.save(xlwt.xls) 70 print the excel save success 71 72 print "----------------分割線 xlutils--------------------" 73 74 from xlutils import copy 75 ‘‘‘xlutils向excel文件中寫入數據,與xlrd結合使用‘‘‘ 76 #打開excel文件 77 rb = xlrd.open_workbook("xlwt.xls") 78 wb = copy.copy(rb) #copy副本進行寫數據 79 #獲取sheet對象,不能通過rb進行sheet的獲取,xlrd沒有write()方法 80 ws = wb.get_sheet(0) 81 ws.write(0, 0, 666666666) 82 print "write success" 83 #必須要保存,保存為同名文件,未修改的部分保留 84 wb.save(xlwt.xls) 85 86 print "----------------分割線 pyExcelerator read--------------------" 87 88 import pyExcelerator as pyExcel 89 ‘‘‘讀excel文件數據,解析excel文件,返回整個excel的數據,返回list‘‘‘ 90 ##parse_xls返回一個列表,每項都是一個sheet頁的數據。 91 #每項是一個二元組(表名,單元格數據)。其中單元格數據為一個字典, 92 #鍵值就是單元格的索引(i,j)。如果某個單元格無數據,那麽就不存在這個值 93 sheets = pyExcel.parse_xls(xlwt.xls) 94 print sheets, type(sheets) 95 96 print "----------------分割線 pyExcelerator write--------------------" 97 98 ‘‘‘pyExcelerator write與xlwt類似,都是新建excel來寫入數據‘‘‘ 99 wb = pyExcel.Workbook() 100 ws = wb.add_sheet(u第一頁) 101 #設置樣式 102 style = pyExcel.XFStyle() 103 font = pyExcel.Font() 104 font.name = Times New Roamn 105 font.bold = True 106 style.font = font 107 #寫入數據,使用樣式 108 ws.write(0, 0, u‘你好, style) 109 print "write success" 110 wb.save(pyExcel.xls)

Python excel讀寫