脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python读取Excel实例详解

python读取Excel实例详解

2021-03-28 00:13幸福清风 Python

这篇文章主要为大家详细介绍了python读取Excel的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

1.操作步骤:

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

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

(3)读取sheet

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

2.示例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xun527/article/details/80241652

延伸 · 阅读

精彩推荐