May 29, 2021 Article blog
When using Python for data grabs or analysis, it is often necessary to read or write data from Excel tables, and today the W3Cschool editor-in-chief shares how Python reads excel tables in the hope that it will help you.
A very compatible package is recommended here, and after use you can access excel table files in a Linux environment, which is xlrs.
After opening the workbook, paste the following code:
import xlrd
wb = xlrd.open_workbook('myworkbook.xls')
Check the form name:
wb.sheet_names()
An index or name is two ways to get the first form:
sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')
After the operation outputs data for each row:
for rownum in range(sh.nrows):
print sh.row_values(rownum)
Just want to get the first column of data:
first_column = sh.col_values(0)
You can also use indexes to get data (starting at 0 by default):
cell_A1 = sh.cell(0,0).value
cell_C4 = sh.cell(rowx=3,colx=2).value
Here's how Python reads the full contents of the excel table, and there are other questions welcome to our Python channel.