xlrd 操作excel

# -*- coding: utf-8 -*-
import xlrd
import numpy as np 


def read_excel(file, sheet_nums, col_num, row_num, cell_index):
    data = xlrd.open_workbook(file)
    for i in range(sheet_nums):
        table = data.sheets()[i] # 通过索引获取工作簿
        # table = data.sheet_by_name(u'Sheet1')  # 通过名称获取 
        nrows = table.nrows  # 获取工作簿的行数
        ncols = table.ncols  # 获取工作簿的列数
        col_val = table.col_values(col_num)
        row_val = table.row_values(row_num)
        for k in range(nrows):
            print(table.row_values(k))
        for j in range(ncols):
            print(table.col_values(j))
        cell_A1 = table.cell(0, 0).value
        cell_val = table.cell(cell_index[0], cell_index[1]).value  # 获取某个单元格的值


if __name__ == '__main__':
    file = ''
    sheet_nums = num1
    col_num = num2
    row_num = num3
    cell_index = (x, y)
    read_excel(file, sheet_nums, col_num, row_num, cell_index)
发布了11 篇原创文章 · 获赞 0 · 访问量 559

猜你喜欢

转载自blog.csdn.net/leo19930725/article/details/105466997