python读取Excel实例详解
2018-08-17 09:00
1101 查看
本文实例为大家分享了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:\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).ctype if__name__ =='__main__': read_excel()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- python 读取excel文件生成sql文件实例详解
- python下读取公私钥做加解密实例详解
- Android 中读取Excel文件实例详解
- python实现读取excel写入mysql的小工具详解
- Python读取Excel的方法实例分析
- Python读取Excel的方法实例分析
- 【python】解析Excel中使用xlrd库、xlwt库操作,读取excel进阶完整脚本实例(四)
- python 读取txt中每行数据,并且保存到excel中的实例
- python读取二进制mnist实例详解
- 详解python实现读取邮件数据并下载附件的实例
- 【python】详解zipfile模块读取处理压缩文件实例
- 【python】pandas库pd.read_excel操作读取excel文件参数整理与实例
- python向已存在的excel中新增表,不覆盖原数据的实例
- Python 迭代器与生成器实例详解
- JExcelAPI读取Excel的实例三
- Python多进程并发(multiprocessing)用法实例详解
- pandas读取excel问题解答 python3-No CODEPAGE record, no encoding_override: will use 'ascii'
- Python读取Excel文件统计演员参演电影
- HTML5 FileReader详解与实例---读取并显示图像文件
- Python对excel写入数据操作实例代码(只供参考)