1. 程式人生 > >excel 資料讀取,資料遍歷,獲取日期資料和合並單元格資料

excel 資料讀取,資料遍歷,獲取日期資料和合並單元格資料

需求:現有一個excel表格,讀取date列的內容和合並單元格的內容

Sheet1頁的資料內容如下:
在這裡插入圖片描述
程式碼如下:

import xlrd
import collections
import json
import operator
from ctypes import *
from datetime import date,datetime

excel_file = xlrd.open_workbook('C:\\Users\\belle.zhao\\Desktop\\test.xlsx','rb')  # 開啟excel檔案
print(excel_file.sheet_names(
)) # 獲取Excel的sheet表 sheet = excel_file.sheet_by_name('Sheet1') # 選擇sheet頁sheet1 sheet1 = excel_file.sheet_by_index(1) #選擇第2個sheet頁的表 title = sheet.row_values(0) #獲取sheet頁的標題 ncols = sheet.ncols # 獲取sheet1 頁的列數 nrows = sheet.nrows # 獲取sheet1頁的行數 data_list = [] for i in range(1,nrows): data_values =
collections.OrderedDict() row_value = sheet.row_values(i) for j in range(0,len(row_value)-1): data_values[title[j]] = row_value[j] print([title[j]],row_value[j]) data_list.append(data_values) row_result = json.dumps(data_list) print("len(row_value)-1列的所有行的值是:", row_result)
cell_value=sheet.cell_value(1,1) # 獲取第2行第2列的值 cell_value2=sheet.cell(1,1) # 獲取第2行第2列的資料型別和值 cell_value3=sheet.row(1)[1] # 獲取第2行第2列的值 print("cell_value的值是:", cell_value) print("cell_value2的值是:",cell_value2) print("cell_value3的值是:",cell_value3) # 讀取excel中日期型別的資料 # 將第4行第4列的資料轉化為日期格式: cell_value_type=sheet.cell_type(3,3) # 獲取第4行第4列的資料型別,表格中是日期型別 if cell_value_type == 3: date_value = xlrd.xldate_as_tuple(sheet.cell_value(3,3), excel_file.datemode) date_tmp = date(*date_value[:3]).strftime('%Y/%m/%d') print("date_value的值是",date_tmp) # 第4列的日期資料轉化為日期格式輸出: col_date=[] for m in range(1,nrows): col_date_value = xlrd.xldate_as_tuple(sheet.cell_value(m,3), excel_file.datemode) col_date.append(col_date_value[0:3]) # 獲取日期列的內容,不要時分秒 print("日期列的內容是:", col_date) # 利用datetime獲取datetime物件型別 col_datetime = [] for n in range(1, nrows): col_date_value2 = xlrd.xldate_as_datetime(sheet.cell(n,3).value, 0) col_datetime.append(col_date_value2) # 獲取日期列的內容,格式為datetime型別 print("日期列的內容col_datetime是:", col_datetime) # 將datetime 物件型別轉化為元組 col_datetime2 = [] for a in range(1, nrows): col_date_value3 = xlrd.xldate_as_datetime(sheet.cell_value(a,3), 0) col_datetime2.append(col_date_value3.__str__()) # 獲取日期列的內容,日期格式轉化為元組 print("日期列的內容col_datetime2是:", col_datetime2) # 將元組轉化為字串 # 讀取合併單元格的資料 print("當前sheet頁的合併單元格有:", sheet.merged_cells) print("第3行第5列合併的單元格的資料是:", sheet.cell_value(3,5)) merge_cell = [] for (rlow,rhigh,clow,chigh) in sheet.merged_cells: merge_cell.append([rlow,clow]) print("獲取合併單元格的起始行和起始列:", merge_cell) print("獲取合併單元格的資料型別:", type(merge_cell)) for index in merge_cell: print("獲取合併單元格的值:", sheet.cell_value(index[0],index[1])) # 第0個索引得到的值做為行,第2個索引得到的值做為列,並進行遍歷

部分執行結果如下:

日期列的內容是: [(2018, 10, 5), (2018, 10, 5), (2018, 9, 5), (2018, 10, 5), (2018, 9, 5), (2018, 10, 5), (2018, 10, 5), (2018, 9, 5), (2018, 11, 5), (2018, 9, 5), (2018, 9, 5), (2018, 7, 5), (2018, 9, 5), (2018, 9, 5), (2018, 10, 5), (2018, 10, 6), (2018, 9, 5), (2018, 11, 5), (2018, 11, 5), (2018, 11, 5), (2018, 9, 5), (2018, 11, 5), (2018, 11, 5), (2018, 7, 5), (2018, 9, 5), (2018, 7, 5)]
日期列的內容col_datetime是: [datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 7, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 10, 5, 0, 0), datetime.datetime(2018, 10, 6, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 7, 5, 0, 0), datetime.datetime(2018, 9, 5, 0, 0), datetime.datetime(2018, 7, 5, 0, 0)]
日期列的內容col_datetime2是: ['2018-10-05 00:00:00', '2018-10-05 00:00:00', '2018-09-05 00:00:00', '2018-10-05 00:00:00', '2018-09-05 00:00:00', '2018-10-05 00:00:00', '2018-10-05 00:00:00', '2018-09-05 00:00:00', '2018-11-05 00:00:00', '2018-09-05 00:00:00', '2018-09-05 00:00:00', '2018-07-05 00:00:00', '2018-09-05 00:00:00', '2018-09-05 00:00:00', '2018-10-05 00:00:00', '2018-10-06 00:00:00', '2018-09-05 00:00:00', '2018-11-05 00:00:00', '2018-11-05 00:00:00', '2018-11-05 00:00:00', '2018-09-05 00:00:00', '2018-11-05 00:00:00', '2018-11-05 00:00:00', '2018-07-05 00:00:00', '2018-09-05 00:00:00', '2018-07-05 00:00:00']
當前sheet頁的合併單元格有: [(3, 6, 5, 6), (9, 11, 2, 3), (1, 4, 4, 5)]3行第5列合併的單元格的資料是: female
獲取合併單元格的起始行和起始列: [[3, 5], [9, 2], [1, 4]]
獲取合併單元格的資料型別: <class 'list'>
獲取合併單元格的值: female
獲取合併單元格的值: 3000.0
獲取合併單元格的值: 24.0