使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取excel是不能对其进行操作的
1.导入xlrd包 import xlrd
2.打开excel文档
table = xlrd.open_workbook("**.xlsx")
3.读取sheet页面数据
①根据下标读取
sheet1 = table.sheets()[0]
②根据sheet_by_index()读取
sheet2=table.sheet_by_index(0)
③根据sheet_by_name()读取
sheet3=table.sheet_by_name(“Sheet1”)
4.读取表的行数和列数
rows=sheet1.nrows
columns=sheet1.ncols
print(rows,columns)
5.读取指定行的值和指定列的值
rowvalue=sheet1.row_values(1)
colvalue=sheet1.col_values(0)
print(rowvalue,colvalue)
cwd = os.getcwd()
if os.path.exists(cwd + '\\lip\\testhndp.xlsx'):
file = cwd + '\\lip\\testhndp.xlsx'
print(file)
table = xlrd.open_workbook(file) #打开文件
sheet1 = table.sheets()[0]
rows = sheet1.nrows
print(rows)
for x in range(1, rows):
row = sheet1.row_values(x)
if row:
cell1 = row[0]
cell2 = row[2]