您好,欢迎访问一九零五行业门户网

python如何导入excel

python语言如何来读取excel文件,分为以下几个操作步骤:
(1)首先安装python官方excel库-->xlrd
首先我们要读取excel要用到xlrd模块,官网安装先上官网安装。
导入模块
import xlrd
(2)获取excel文件位置并读取
相关推荐:《python视频教程》
(3)读取sheet
(4)读取指定rows和cols内容
例:
# -*- coding: utf-8 -*-import xlrdfrom datetime import date,datetimedef read_excel():#文件位置excelfile=xlrd.open_workbook(r'c:\users\administrator\desktop\testdata.xlsx')#获取目标excel文件sheet名print excelfile.sheet_names()#------------------------------------#若有多个sheet,则需要指定读取目标sheet例如读取sheet2#sheet2_name=excelfile.sheet_names()[1]#------------------------------------#获取sheet内容【1.根据sheet索引2.根据sheet名称】#sheet=excelfile.sheet_by_index(1)sheet=excelfile.sheet_by_name('testcase002')#打印sheet的名称,行数,列数print sheet.name,sheet.nrows,sheet.ncols#获取整行或者整列的值rows=sheet.row_values(2)#第三行内容cols=sheet.col_values(1)#第二列内容print cols,rows#获取单元格内容print sheet.cell(1,0).value.encode('utf-8')print sheet.cell_value(1,0).encode('utf-8')print sheet.row(1)[0].value.encode('utf-8')#打印单元格内容格式print sheet.cell(1,0).ctypeif__name__ =='__main__':read_excel()
以上就是python如何导入excel的详细内容。
其它类似信息

推荐信息