search
HomeBackend DevelopmentPython TutorialIntroduction to the method of processing Excel xlrd in python

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

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])) # 打印日期

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!

Statement
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
How do you slice a Python array?How do you slice a Python array?May 01, 2025 am 12:18 AM

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Under what circumstances might lists perform better than arrays?Under what circumstances might lists perform better than arrays?May 01, 2025 am 12:06 AM

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

How can you convert a Python array to a Python list?How can you convert a Python array to a Python list?May 01, 2025 am 12:05 AM

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

What is the purpose of using arrays when lists exist in Python?What is the purpose of using arrays when lists exist in Python?May 01, 2025 am 12:04 AM

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

Explain how to iterate through the elements of a list and an array.Explain how to iterate through the elements of a list and an array.May 01, 2025 am 12:01 AM

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment