码迷,mamicode.com
首页 > 编程语言 > 详细

Python 读取excel文件

时间:2019-06-19 09:54:37      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:class   exce   需要   install   打开   append   nbsp   mic   book   

python 读取excel数据

 

pip install xlrd

 

import xlrd


def read_excel():
    """
    读取Excel文件
    :return:
    """
    # 打开excel文件进行数据提取
    excel_file = xlrd.open_workbook(demo/test.xlsx)

    # 获取Excel文件sheet名
    excel_file.sheet_names()
    print(excel_file.sheet_names())
    # 有多个sheet时需要指定读取目标sheet
    sheet1_name = excel_file.sheet_names()[0]
    print(sheet1_name)
    # 获取sheet1内容
    # 通过index读取
    # sheet1_info = excel_file.sheet_by_index(0)
    # 通过对应的名字读取
    sheet1_info = excel_file.sheet_by_name(Sheet1)

    # 读取Excel文件所有数据
    values = []
    for i in range(sheet1_info.nrows):
        print(sheet1_info.row_values(i))
        values.append(sheet1_info.row_values(i))

    # 获取指定行和列数据
    rows = sheet1_info.row_values(1)
    cols = sheet1_info.col_values(2)
    # 获取表格里的内容,三种方式
    print(sheet1_info.cell(1, 2).value)  # 获取第二行第三列数据
    print(sheet1_info.cell_value(1, 0))
    print(sheet1_info.row(1)[0].value)

    print(rows, cols)

 test.xlsx文件

技术图片

 

Python 读取excel文件

标签:class   exce   需要   install   打开   append   nbsp   mic   book   

原文地址:https://www.cnblogs.com/CesareZhang/p/11049553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!