目的:
python能使用xlrd模塊實(shí)現(xiàn)對(duì)Excel數(shù)據(jù)的讀取,且按照想要的輸出形式。
總體思路:
(1)要想實(shí)現(xiàn)對(duì)Excel數(shù)據(jù)的讀取,需要用到第三方應(yīng)用,直接應(yīng)用。
(2)實(shí)際操作時(shí)候和我們實(shí)際平時(shí)打開一個(gè)文件進(jìn)行操作一樣,先找到文件-->打開文件-->定義要讀取的sheet-->讀取出內(nèi)容。
Excel處理合并單元格:
已存在合并單元格如下:
xlrd中的 merged_cells 屬性介紹:[code]import xlrd
1
2
3
4
5
|
import xlrd workbook = xlrd.open_workbook( './data/test_data.xlsx' ) sheet = workbook.sheet_by_name( 'Sheet1' ) merged = sheet.merged_cells # 返回一個(gè)列表 起始行,結(jié)束行,起始列,結(jié)束列) print (merged) |
讀取合并單元格中的某一個(gè)單元格的值編寫成一個(gè)方法:
1
2
3
4
5
6
7
8
9
|
def get_merged_cell_value(row_index,col_index): cell_value = None for (rlow, rhigh, clow, chigh) in merged: if (row_index > = rlow and row_index < rhigh): if (col_index > = clow and col_index < chigh): cell_value = sheet.cell_value(rlow, clow) return cell_value print ( get_merged_cell_value( 0 , 1 ) ) |
給出坐標(biāo),判斷是否為合并單元格:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#方法參數(shù)為單元格的坐標(biāo)(x,y),如果給的坐標(biāo)是合并的單元格,輸出此單元格是合并的,否則,輸出普通單元格 def get_merged_cell_value(row_index,col_index): for (rlow, rhigh, clow, chigh) in merged: if (row_index > = rlow and row_index < rhigh and col_index > = clow and col_index < chigh): print ( "此單元格是合并單元格" ) else : print ( "此單元格為普通單元格" ) print ( get_merged_cell_value( 4 , 3 ) ) ##讀取第3列的所有數(shù)據(jù),并進(jìn)行降序排序 clox = 3 list1 = [] for i in range ( 1 ,sheet.nrows): cell_value = float (sheet.cell_value(i,clox)) list1.append(cell_value) print (list1) list1.sort() list1.reverse() print (list1) |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/123anqier-blog/p/13234406.html