python读取excel文件

1.引入库 xlrd

import xlrd

2.打开文件

workBook = xlrd.open_workbook(r'文件位置/名字.xlsx')

3.1 .获取sheet名字

# 1.1 获取所有sheet的名字(list类型)
    allSheetNames = workBook.sheet_names()
    print(allSheetNames)

输出:

3.2  按索引号获取sheet的名字(string类型)

sheet1Name = workBook.sheet_names()[0]
print(sheet1Name)

输出:

4.获取sheet内容

#法1:按索引号获取sheet内容
sheet1_content1 = workBook.sheet_by_index(0) # sheet索引从0开始
##法2:按sheet名字获取sheet内容
sheet1_content2 = workBook.sheet_by_name('sheet1')

5.获取整行和整列的值(数组)

print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)
# 获取整行和整列的值(数组)
rows = sheet1_content1.row_values(3) # 获取第四行内容
cols = sheet1_content1.col_values(2) # 获取第三列内容
print(rows)

输出:

6.获取单元格内容(三种方式)

print(sheet1_content1.cell(1, 0).value)
print(sheet1_content1.cell_value(2, 2))
print(sheet1_content1.row(2)[2].value)

输出:

 

7.获取单元格内容的数据类型

# Tips: python读取excel中单元格的内容返回的有5种类型 [0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error]
print(sheet1_content1.cell(1, 0).ctype)

 

完整代码

import xlrd


def read_excel():
    # 打开文件
    workBook = xlrd.open_workbook(r'D:/毕业/procese/2022-05-05-08训练过程数据(lr0.001000changeconv1d).xlsx')

    # 1.获取sheet的名字
    # 1.1 获取所有sheet的名字(list类型)
    allSheetNames = workBook.sheet_names()
    print(allSheetNames)

    # 1.2 按索引号获取sheet的名字(string类型)
    sheet1Name = workBook.sheet_names()[0]
    print(sheet1Name)

    # 2. 获取sheet内容
    ## 2.1 法1:按索引号获取sheet内容
    sheet1_content1 = workBook.sheet_by_index(0) # sheet索引从0开始
    ## 2.2 法2:按sheet名字获取sheet内容
    sheet1_content2 = workBook.sheet_by_name('sheet1')

    # 3. sheet的名称,行数,列数
    print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)

    # 4. 获取整行和整列的值(数组)
    rows = sheet1_content1.row_values(3) # 获取第四行内容
    cols = sheet1_content1.col_values(2) # 获取第三列内容
    print(rows)

    # 5. 获取单元格内容(三种方式)
    print(sheet1_content1.cell(1, 0).value)
    print(sheet1_content1.cell_value(2, 2))
    print(sheet1_content1.row(2)[2].value)

    # 6. 获取单元格内容的数据类型
    # Tips: python读取excel中单元格的内容返回的有5种类型 [0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error]
    print(sheet1_content1.cell(1, 0).ctype)


if __name__ == '__main__':
    read_excel()

输出:

python读取excel文件_第1张图片

 

你可能感兴趣的:(论文相关,python)