栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python读取Excel实例详解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python读取Excel实例详解

本文实例为大家分享了python读取Excel实例的具体代码,供大家参考,具体内容如下

1.操作步骤:

(1)安装python官方Excel库-->xlrd

(2)获取Excel文件位置并读取

(3)读取sheet

(4)读取指定rows和cols内容

2.示例代码:

# -*- coding: utf-8 -*-
 
import xlrd 
from datetime import date,datetime
 
def read_excel():
 
#文件位置
ExcelFile=xlrd.open_workbook(r'C:UsersAdministratorDesktopTestData.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).ctype
 
if__name__ =='__main__':
 
read_excel()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/28922.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号