Home > Backend Development > Python Tutorial > Introduction to the method of processing Excel xlrd in python

Introduction to the method of processing Excel xlrd in python

巴扎黑
Release: 2017-09-13 09:47:22
Original
3335 people have browsed it

This article mainly introduces the relevant information of python to process Excel in detail. The simple use of xlrd has certain reference value. Interested friends can refer to it

xlrd is mainly used for reading Get the Excel file. This article shares the specific code for python to process Excel for your reference. The specific content is as follows

Installation


# #

pip install xlrd
Copy after login

api use


import xlrd

# 打开Excel文件读取数据
workbook = xlrd.open_workbook('a.xlsx');

# 打印所有的sheet列出所有的sheet名字
print(workbook.sheet_names())

# 根据sheet索引或者名称获取sheet内容
Data_sheet = workbook.sheets()[0];
# Data_sheet = workbook.sheet_by_index(1) 
# Data_sheet = workbook.sheet_by_name(u'Charts') 

# 获取sheet名称、行数和列数
print(Data_sheet.name,Data_sheet.nrows,Data_sheet.ncols)


# 获取整行和整列的值(列表)   
rows = Data_sheet.row_values(0) #获取第一行内容 
cols = Data_sheet.col_values(1) #获取第二列内容 
print(rows)
print(cols)

# 获取单元格内容的数据类型
# 相当于在一个二维矩阵中取值
# (row,col)-->(行,列)
cell_A1 = Data_sheet.cell(0,0).value # 第一行第一列坐标A1的单元格数据
# cell_C1 = Data_sheet.cell(0,2).value # 第一行第三列坐标C1的单元格数据

# cell_B1 = Data_sheet.row(0)[1].value # 第1行第2列
# cell_D2 = Data_sheet.col(3)[1].value # 第4列第2行

# 检查单元格的数据类型
# ctype的取值含义
# ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
print(Data_sheet.cell(4,0).ctype) 

# 读取excel中单元格内容为日期的方式
date_value = xlrd.xldate_as_tuple(Data_sheet.cell_value(4,0),workbook.datemode)

print(date_value) # -->(2017, 9, 6, 0, 0, 0)

print('%d:%d:%d' %(date_value[3:])) # 打印时间
print('%d/%02d/%02d' %(date_value[0:3])) # 打印日期
Copy after login

The above is the detailed content of Introduction to the method of processing Excel xlrd in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template