xlrd-讀取xls和xlsx格式的excel文件


sheet相關代碼:

 1 import  xlrd
 2 workbook=xlrd.open_workbook('1224.xlsx',on_demand=True)#打開文件
 3 sheet_name=workbook.sheet_names()  #所有sheet的名字
 4 print(sheet_name)
 5 print("*"*100)
 6 sheets=workbook.sheets()#返回可迭代的sheets對象
 7 for i ,sheet in enumerate(sheets):
 8     print("the sheet{0}\'s sheetname is {1}".format(i+1,sheet.name))
 9 print(workbook.nsheets)#獲取sheet的個數
10 print("*"*100)
11 print(sheets[0].name)
12 print("*"*100)
13 print(workbook.sheet_by_index(3).name)
14 print("*"*100)
15 print(workbook.sheet_by_name('Index').number)
16 #注意:work.sheet_by_name和index獲得的是對象
17 print(workbook.sheet_by_name('Index'))
18 print(workbook.sheet_by_index(3))

結果:

['TemplateInfo', 'Index', 'GBssFunction', 'GSiteBaseBandShare', 'GMocnArea', 'GBssIftaMa', 'GGsmCell', 'GCellIfta', 'GHandoverControl', 'GPsHandoverControl']
****************************************************************************************************
the sheet1's sheetname is TemplateInfo
the sheet2's sheetname is Index
the sheet3's sheetname is GBssFunction
the sheet4's sheetname is GSiteBaseBandShare
the sheet5's sheetname is GMocnArea
the sheet6's sheetname is GBssIftaMa
the sheet7's sheetname is GGsmCell
the sheet8's sheetname is GCellIfta
the sheet9's sheetname is GHandoverControl
the sheet10's sheetname is GPsHandoverControl
10
****************************************************************************************************
TemplateInfo
****************************************************************************************************
GSiteBaseBandShare
****************************************************************************************************
1
<xlrd.sheet.Sheet object at 0x02F2ECB0>
<xlrd.sheet.Sheet object at 0x02F268B0>

Process finished with exit code 0

2.行列相關代碼:

 1 import  xlrd
 2 workbook=xlrd.open_workbook('1224.xlsx',on_demand=True)#打開文件
 3 sheet_name=workbook.sheet_names()  #所有sheet的名字
 4 i=0
 5 while i <workbook.nsheets:
 6     sheet_name=workbook .sheet_by_index(i).name
 7     nrows=workbook.sheet_by_index(i).nrows
 8     ncols=workbook.sheet_by_index(i).ncols
 9     print("{0} has {1} rows and {2} cols".format(sheet_name,nrows,ncols))
10     i+=1
11 print("*"*100)
12 print(workbook .sheet_by_index(5).col(5))
13 print("*"*100)
14 print(workbook.sheet_by_index(5).row_values(1))
15 print("*"*100)
16 print(workbook.sheet_by_index(5).col_values(1))

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
TemplateInfo has 6 rows and 2 cols
Index has 31 rows and 256 cols
GBssFunction has 6 rows and 164 cols
GSiteBaseBandShare has 9 rows and 169 cols
GMocnArea has 5 rows and 8 cols
GBssIftaMa has 5 rows and 6 cols
GGsmCell has 788 rows and 167 cols
GCellIfta has 788 rows and 7 cols
GHandoverControl has 788 rows and 166 cols
GPsHandoverControl has 788 rows and 9 cols
****************************************************************************************************
[text:'MaArfcnList', text:'Frequency List', text:'long[16][0..1023]', text:'Arfcn of MAARFCNLIST is separated by semicolon', text:'R-W-I']
****************************************************************************************************
['RESULT', 'Modification Indication', 'Managed Element Object ID', 'IFTAMA ID', 'Frequency Band of IftaMa', 'Frequency List']
****************************************************************************************************
['MODIND', 'Modification Indication', 'A,D,M,P', 'A:Add, D:Delete, M:Modify, P:Pass', '']

Process finished with exit code 0

單元格的值:

 1 import  xlrd
 2 workbook=xlrd.open_workbook('1224.xlsx',on_demand=True)#打開文件
 3 sheet_name=workbook.sheet_names()  #所有sheet的名字
 4 i=0
 5 sheet_name = workbook.sheet_by_index(5).name
 6 nrows = workbook.sheet_by_index(5).nrows
 7 ncols = workbook.sheet_by_index(5).ncols
 8 print(nrows,ncols)
 9 while i <nrows:
10     j=0
11     while j <ncols:
12         print(workbook.sheet_by_index(5).cell(i,j),end="")
13         j+=1
14     print("")
15     i += 1
16 print("*"*100)
17 print(workbook.sheet_by_index(5).cell(4,3))
18 print("*"*100)
19 print(workbook.sheet_by_index(5).cell(4,3).value)
20 print("*"*100)
21 print(workbook.sheet_by_index(5).cell_value(4,3))
22 print("*"*100)
23 print(workbook.sheet_by_index(5).row(4)[3].value)
24 print("*"*100)
25 print(workbook.sheet_by_index(5).col(3)[4].value)

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
5 6
text:'RESULT'text:'MODIND'text:'MEID'text:'GBssIftaMaId'text:'FreqBand'text:'MaArfcnList'
text:'RESULT'text:'Modification Indication'text:'Managed Element Object ID'text:'IFTAMA ID'text:'Frequency Band of IftaMa'text:'Frequency List'
text:'S:Success\nF:Fail'text:'A,D,M,P'text:'long:[1..4095]'text:'long:[1..1]'text:'0:GSM900\\(0 .. 124,975 .. 1023\\)\n1:EGSM900\\(0 .. 124,975 .. 1023\\)\n2:DCS1800\\(512 .. 885\\)\n3:RGSM\\(0 .. 124,955 .. 1023\\)\n4:PCS1900\\(512 .. 810\\)\n7:GSM850\\(128..251\\)'text:'long[16][0..1023]'
text:''text:'A:Add, D:Delete, M:Modify, P:Pass'text:''text:''text:''text:'Arfcn of MAARFCNLIST is separated by semicolon'
text:''text:''text:'Primary Key'text:'Primary Key'text:'R-W-I'text:'R-W-I'
****************************************************************************************************
text:'Primary Key'
****************************************************************************************************
Primary Key
****************************************************************************************************
Primary Key
****************************************************************************************************
Primary Key
****************************************************************************************************
Primary Key

Process finished with exit code 0

 多行多列,制定行列切片的值:

 1 import  xlrd
 2 workbook=xlrd.open_workbook('1224.xlsx',on_demand=True)#打開文件
 3 nrows = workbook.sheet_by_index(5).nrows
 4 ncols = workbook.sheet_by_index(5).ncols
 5 print(nrows,ncols)
 6 sheet=workbook.sheets()[5]
 7 #row_values(rowx,start_colx=0,end_colx=None)
 8 print(sheet.row_values(4,3,5))
 9 #col_values(colx,start_rowx=0,end_rowx=None)
10 print(sheet.col_values(4,3,5))

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
5 6
['Primary Key', 'R-W-I']
['', 'R-W-I']

Process finished with exit code 0

讀取多行多列的值:

1 def get_rows(sheet,start_rowx=0,end_rows=0):
2     rows_value=[sheet.row_values(rowx) for rowx in range (start_rowx,end_rows)]
3     return rows_value
4 print(get_rows(sheet,4,5))
5 def get_cols(sheet,start_colx=0,end_colx=0):
6     cols_value=[sheet.col_values(colx) for colx in range (start_colx,end_colx)]
7     return cols_value
8 print(get_cols(sheet,1,3))

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
5 6
[['', '', 'Primary Key', 'Primary Key', 'R-W-I', 'R-W-I']]
[['MODIND', 'Modification Indication', 'A,D,M,P', 'A:Add, D:Delete, M:Modify, P:Pass', ''], ['MEID', 'Managed Element Object ID', 'long:[1..4095]', '', 'Primary Key']]

Process finished with exit code 0

獲得連續區域的值:

 1 import  xlrd
 2 workbook=xlrd.open_workbook('1224.xlsx',on_demand=True)#打開文件
 3 nrows = workbook.sheet_by_index(5).nrows
 4 ncols = workbook.sheet_by_index(5).ncols
 5 print(nrows,ncols)
 6 sheet=workbook.sheets()[5]
 7 
 8 def get_range(sheet,start_rowx=0,end_rowx=0,start_colx=0,end_colx=0):
 9     range_values=[sheet.row_values(rowx,start_colx,end_colx)for rowx in range(start_rowx,end_rowx)]
10     return range_values
11 print(get_range(sheet,0,1,1,3))

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
5 6
[['MODIND', 'MEID']]

Process finished with exit code 0

 讀取單個sheet頁的值

 1 import  xlrd
 2 def readsheet(filename,sheetindex=0,sheetname=None):
 3         # 讀取Excel文件的一個sheet頁
 4     workbook=xlrd.open_workbook(filename,on_demand=True)
 5     if  sheetname is not None:
 6         sheet=workbook.sheet_by_name(sheetname)
 7     else:
 8         sheet=workbook.sheet_by_index(sheetindex)
 9     rows=[sheet.row_values(row)for row in range(sheet.nrows)]
10     return rows
11 ret=readsheet('1224.xlsx',sheetindex=0)
12 print(ret)

結果:

C:\Users\zte\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/zte/PycharmProjects/radioparametercheck/workbook.py
[['NE Type:', 'Multi-mode Controller'], ['Template Type:', 'Plan'], ['Template Version:', 'V12.11'], ['Data Type:', 'gsm_radio'], ['', ''], ['', '']]

Process finished with exit code 0

讀取整個excel

 1 import  xlrd
 2 def readbook(filename):
 3     """
 4     讀取整個Excel文件
 5     :param filename:
 6     :return: sheet名字列表
 7     字典類型的數據 bookdata={sheetname1:row1,sheetname2:row2}
 8     """
 9     workbook=xlrd.open_workbook(filename,on_demand=True)
10     sheetnames=workbook.sheet_names()
11     bookdata={}
12     for sheetname in sheetnames:
13         sheet=workbook.sheet_by_name(sheetname)
14         rows=[sheet.row_values(row)for row in range(sheet.nrows)]
15         bookdata.setdefault(sheetname,rows)
16     return sheetnames,bookdata
17 print(readbook("1224.xlsx"))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM