xlrd部分类方法说明

2024-01-17 05:48
文章标签 方法 说明 部分 xlrd

本文主要是介绍xlrd部分类方法说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用python读取excle,需安装第三方模块xlrd

下载地址:

https://pypi.python.org/pypi/xlrd

安装过程略,安装成功后,就可调用xlrd的方法进行去读EXCEL文件操作

cellname(rowx, colx) 

显示单元格名称

print xlrd.cellname(0,0)

>>> A1

cellnameabs(rowx, colx, r1c1=0) 

格式化显示单元格名称

print xlrd.cellnameabs(0,0,r1c1=0)

>>> $A$1

colname(colx) 

显示列名称

print xlrd.colname(0)

>>> A

empty_cell (variable)

空单元格

print xlrd.empty_cell()

>>> empty:''

error_text_from_code (variable)

报错信息

print xlrd.error_text_from_code

print xlrd.error_text_from_code[7]

>>> {0: '#NULL!', 36: '#NUM!', 7: '#DIV/0!', 42: '#N/A', 15: '#VALUE!', 23: '#REF!', 29: '#NAME?'}

>>> #DIV/0!

open_workbook(filename=None, logfile=sys.stdout, verbosity=0, pickleable=True,use_mmap=USE_MMAP,file_contents=None, encoding_override=None, formatting_info=False, on_demand=False, ragged_rows=False, ) 

打开一个EXCEL文件,返回一个Book类的实例

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

print book

>>> <xlrd.book.Book object at 0x01D0F810>

xldate_as_tuple(xldate, datemode) 

转换数值为时间格式。0:1900-base 1:1904-based

print table1.cell(1,1).value

print xlrd.xldate_as_tuple(table1.cell(1,1).value, 0)

>>> 200.0

>>> (1900, 7, 18, 0, 0, 0)

biff_version 

BIFF (Binary Interchange File Format)版本。最新的为80,支持EXCEL97.

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

print book.biff_version

>>> 80

codepage 

显示文档使用字符编码 1200 代表 Unicode

encoding 

同样显示字符编码

print book.codepage

print book.encoding

>>> 1200

>>> tuf_16_le

nsheets 

文件总sheet数

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

print book.nsheets

>>> 3

sheet_by_index(sheetx) 

通过序号索引sheet页,返回一个Sheet类的实例

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

table=[]

for i in range(book.nsheets):

    table.append(book.sheet_by_index(i))

print table[i]

>>> <xlrd.sheet.Sheet object at 0x01BD3270>

>>> <xlrd.sheet.Sheet object at 0x01BD3570>

>>> <xlrd.sheet.Sheet object at 0x01BD35D0>

sheet_by_name(sheet_name) 

通过页名称索引sheet页,返回一个Sheet类的实例

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

table = book.sheet_by_name("Sheet1")

print table

>>> <xlrd.sheet.Sheet object at 0x01B882B0>

sheet_loaded(sheet_name_or_index) 

通过名称或编号,载入sheet。成功则返回True,失败则报错

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

table0 = book.sheet_loaded(0)

table1 = book.sheet_loaded("Sheet2")

print table0,table1

>>> True True

sheet_names() 

返回sheet名称列表

name = book.sheet_names()

print name

>>> [u'Sheet1', u'Sheet2', u'Sheet3']

 

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

table=[]

name = book.sheet_names()

for n in name:

    table.append(book.sheet_by_name(n))

print table

>>> [<xlrd.sheet.Sheet object at 0x01C23270>, <xlrd.sheet.Sheet object at 0x01C23570>, <xlrd.sheet.Sheet object at 0x01C235D0>]

sheets() 

返回所有sheet,返回值为Sheet的实例

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

table = book.sheets()

print table

>>> [<xlrd.sheet.Sheet object at 0x01C8A290>, <xlrd.sheet.Sheet object at 0x01C8A590>, <xlrd.sheet.Sheet object at 0x01C8A5F0>]

unload_sheet(sheet_name_or_index) 

通过名称或编号,卸载sheet。成功则返回None,失败则报错

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

print book.unload_sheet(2)

 

book 

sheet类实例调用所属的Book对象属性

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

sheet = book.sheet_by_index(0)

print sheet.book.nsheets

>>> 3

cell(rowx, colx) 

根据横纵值,返回cell对象

cell_value(rowx, colx)

根据横纵值,返回cell值

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

sheet = book.sheet_by_index(0)

print sheet.cell(0,0)

print sheet.cell_value(0,0)

>>> text:u'\u59d3\u540d'

>>> 姓名

col(colx)

返回指定列的cell对象列表

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

sheet = book.sheet_by_index(0)

print sheet.col(0)

>>> [text:u'\u59d3\u540d', text:u'\u5f20\u4e09', text:u'\u674e\u56db', text:u'\u738b\u4e94']

col_values(colx, start_rowx=0, end_rowx=None)

查询指定列的值,返回list对象

book = xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls")

sheet = book.sheet_by_index(0)

print sheet.col_values(0)

>>> [u'\u59d3\u540d', u'\u5f20\u4e09', u'\u674e\u56db', u'\u738b\u4e94']

computed_column_width(colx)

查询指定列宽,需要打开列表属性formatting_info=True

book=xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls",formatting_info=True)

sheet = book.sheet_by_index(0)

print sheet.computed_column_width(0)

>>> 2656

name

查询表名

ncols

查询列数

nrows

查询行数

book=xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls",formatting_info=True)

sheet = book.sheet_by_index(0)

print sheet.name

print sheet.ncols

print sheet.nrows

>>> sheet1

>>> 5

>>> 4

row(rowx)

查询指定行,返回cell类实例列表

row_len(rowx)

查询指定行的列数

row_values(rowx, start_colx=0, end_colx=None)

查询指定行的值

book=xlrd.open_workbook(r"C:\Users\Administrator\Desktop\506\test.xls",formatting_info=True)

sheet = book.sheet_by_index(0)

print sheet.row(0)

print sheet.row_len(0)

print sheet.row_values(0)

>>> [text:u'\u59d3\u540d' (XF:15), text:u'\u5e74\u9f84' (XF:15), text:u'\u5de5\u4f5c' (XF:15), text:u'\u5de5\u8d44' (XF:15), text:u'\u5de5\u65f6' (XF:15)]

>>> 5

>>> [u'\u59d3\u540d', u'\u5e74\u9f84', u'\u5de5\u4f5c', u'\u5de5\u8d44', u'\u5de5\u65f6']

 

通过Sheet类的方法可以获得cell对象,cell对象有三个属性

ctype int类型 代表文本格式

Type symbol

Type number

Python value

XL_CELL_EMPTY

0

empty string u''

XL_CELL_TEXT

1

a Unicode string

XL_CELL_NUMBER

2

float

XL_CELL_DATE

3

float

XL_CELL_BOOLEAN

4

int; 1 means TRUE, 0 means FALSE

XL_CELL_ERROR

5

int representing internal Excel codes; for a text representation, refer to the supplied dictionary error_text_from_code

XL_CELL_BLANK

6

empty string u''. Note: this type will appear only when open_workbook(..., formatting_info=True) is used.

 

value 文本内容,取决于ctype格式

xf_index 如果打开文件时 formatting_info不为True,xf_indexNone

 

merged_cells

返回sheet中合并单元格位置,返回值为list,每个单元格信息为元祖,4个元素代表开始行,结束行+1,开始列,结束列+1

sheet = book.sheet_by_name('table2')
row_lo,row_hi,col_lo,col_hi = sheet.merged_cells[0]
print sheet.cell(row_lo,col_lo).value
print sheet.merged_cells

>>> 3 7 2 5
>>> MERGED
>>> [(3, 7, 2, 5), (8, 10, 2, 4)]


这篇关于xlrd部分类方法说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/615035

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

zookeeper端口说明及介绍

《zookeeper端口说明及介绍》:本文主要介绍zookeeper端口说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、zookeeper有三个端口(可以修改)aVNMqvZ二、3个端口的作用三、部署时注意总China编程结一、zookeeper有三个端口(可以

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP