python使用xlrd模块读取excel数据并保存到数据库

import xlrd


file = r"企查查1.xls"


def read_excel():
	‘’‘使用介绍’‘’
    # 获取book对象
    book = xlrd.open_workbook(filename=file)
    # 获取sheet对象
    sheet_name = book.sheet_names()[0] # 获取指定索引的sheet的名字
    print(sheet_name)
    # sheet0、sheet1为两种方法获取的sheet对象
    sheet1 = book.sheet_by_name(sheet_name) # 通过sheet名字来获取sheet对象
    sheet0 = book.sheet_by_index(0)  # 通过sheet索引获取sheet对象
    nrows = sheet0.nrows  # 总行数
    ncolumns = sheet0.ncols  # 总列数
    # 获取指定行、列的值,返回一个值列表
    row_data = sheet0.row_values(2) # 获取第一行的数据列表
    col_data = sheet0.col_values(0) # 获取第一列的数据列表
    cell_value1 = sheet0.cell_value(0,1)  # 只获取cell中的内容
    print(cell_value1)
    cell_value2 = sheet0.cell_value(0,1)
    print(cell_value2)

你可能感兴趣的:(python使用xlrd模块读取excel数据并保存到数据库)