To read an excel file using python, a solution is to use the python module called xlrd. An example with a file called 'read_excel_file_with_python.xlsx':
Table of contents
Get the spreadsheet names
import xlrdimport numpy as npworkbook = xlrd.open_workbook('read_excel_file_with_python.xlsx')SheetNameList = workbook.sheet_names()for i in np.arange( len(SheetNameList) ):print( SheetNameList[i] )
here the document has two spreadsheets:
Personal DataPublic Data
Select a spreadsheet:
worksheet = workbook.sheet_by_name(SheetNameList[0])num_rows = worksheet.nrowsnum_cells = worksheet.ncolsprint( 'num_rows, num_cells', num_rows, num_cells )
returns
num_rows, num_cells 6 4
Read a spreadsheet
curr_row = 0while curr_row < num_rows:row = worksheet.row(curr_row)#print row, len(row), row[0], row[1]print( 'Row: ', curr_row )print( row, len(row), row[0] )curr_cell = 0while curr_cell < num_cells:# Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blankcell_type = worksheet.cell_type(curr_row, curr_cell)cell_value = worksheet.cell_value(curr_row, curr_cell)print( ' ', cell_type, ':', cell_value )curr_cell += 1curr_row += 1
returns
Row: 0[text:'First Name', text:'Last Name', text:'Age', text:'Height'] 4 text:'First Name'1 : First Name1 : Last Name1 : Age1 : HeightRow: 1[text:'John', text:'Doe', number:24.0, number:190.0] 4 text:'John'1 : John1 : Doe2 : 24.02 : 190.0Row: 2[text:'Elijah', text:'Baley', number:31.0, number:169.0] 4 text:'Elijah'1 : Elijah1 : Baley2 : 31.02 : 169.0Row: 3[text:'Paul', text:'Edison', number:22.0, number:185.0] 4 text:'Paul'1 : Paul1 : Edison2 : 22.02 : 185.0Row: 4[text:'Martin', text:'Cage', number:41.0, number:176.0] 4 text:'Martin'1 : Martin1 : Cage2 : 41.02 : 176.0Row: 5[text:'Robert', text:'Lemon', number:32.0, number:195.0] 4 text:'Robert'1 : Robert1 : Lemon2 : 32.02 : 195.0
References
| Links | Site |
|---|---|
| xlrd | pypi.org |
| Lien externe How to read els file in python ? | youlikeprogramming.com |
| Python - Write to Excel Spreadsheet | stackoverflow |
