做纪录片卖给视频网站,个人博客首页,php网站建设素材,成都感染人数最新消息目录
一、xlrd库的由来
二、xlrd库优缺点
1、优点
1-1、支持多种Excel文件格式
1-2、高效性
1-3、开源性
1-4、简单易用
1-5、良好的兼容性
2、缺点
2-1、对.xlsx格式支持有限
2-2、功能相对单一
2-3、更新和维护频率低
2-4、依赖外部资源
三、xlrd库的版本说明 …目录
一、xlrd库的由来
二、xlrd库优缺点
1、优点
1-1、支持多种Excel文件格式
1-2、高效性
1-3、开源性
1-4、简单易用
1-5、良好的兼容性
2、缺点
2-1、对.xlsx格式支持有限
2-2、功能相对单一
2-3、更新和维护频率低
2-4、依赖外部资源
三、xlrd库的版本说明
1、xlrd 1.2.0版本
2、xlrd 2.0.1版本
3、xlrd3(非官方名称)
四、如何学好xlrd库
1、获取xlrd库的属性和方法
2、获取xlrd库的帮助信息
3、实战案例
3-38、通过索引引用工作表
3-39、通过名称引用工作表
3-40、通过sheet_names集合引用工作表
3-41、引用当前活动工作表
3-42、引用第一个工作表
3-43、引用最后一个工作表
3-44、引用所有的工作表
3-45、引用指定的多个工作表
3-46、引用新建的工作表
3-47、引用包含特定单元格的工作表
3-48、引用工作表名称中包含特定字符串的工作表
五、推荐阅读
1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页 在Excel中通常所说的“情侣键”并非官方术语而是对某些常用且经常成对出现的快捷键的一种形象化的称呼。其中最为人熟知和广泛使用的“情侣键”是“CtrlC”和“CtrlV”。
1、CtrlC这个快捷键的作用是“拷贝”或“复制”。当你在Excel中选中某个单元格、一行、一列或整个工作表的内容后按下CtrlC键这些内容就会被复制到计算机的剪贴板中等待下一步的粘贴操作。2、CtrlV这个快捷键的作用是“粘贴”。在你按下CtrlC键将内容复制到剪贴板后可以通过按下CtrlV键将这些内容粘贴到Excel中的另一个位置这两个操作经常是连续进行的因此CtrlC和CtrlV就像一对“情侣”总是成对出现。 除了这对常见的“情侣键”外Excel中还有许多其他的快捷键可以帮助用户更高效地完成各种操作。然而这些快捷键通常并没有像CtrlC和CtrlV那样形成特定的“情侣”关系。 然而今天我不再展开介绍“情侣键”而是要重点推介Python中的“情侣库”即xlrd和xlwt两个第三方库。
一、xlrd库的由来 xlrd库是一种用于在Python中读取Excel文件的库它的名称中的xl代表Excelrd代表读取其开发者是John Machin(注库名字符拆分诠释只是一种猜测)。 xlrd最初是在2005年开始开发的是基于Python的开源项目(下载xlrd库官网下载)。 由于Excel文件在数据处理和分析中的重要性xlrd库填补了Python在处理Excel文件方面的空白使得用户可以方便地在Python环境中读取Excel文件的内容并进行进一步的数据操作和分析。
二、xlrd库优缺点
1、优点
1-1、支持多种Excel文件格式 xlrd库支持多种Excel文件格式包括.xls和.xlsx(在旧版本中)这使得无论数据存储在哪种格式的Excel文件中用户都可以使用xlrd库来读取。
1-2、高效性 xlrd库使用C语言编写因此其性能非常高即使面对非常大的Excel文件xlrd也可以快速地读取其中的数据。
1-3、开源性 xlrd是完全开源的可以在GitHub等平台上找到其源代码这使得任何人都可以根据自己的需求对其进行修改和扩展。
1-4、简单易用 xlrd提供了简单直接的API来获取单元格数据、行列数等使得从Excel文件中读取数据变得简单而高效。
1-5、良好的兼容性 xlrd库适配多种Python版本包括Python 2.7(不包括3.0-3.3)或Python 3.4及以上版本这为用户提供了广泛的兼容性选择。
2、缺点
2-1、对.xlsx格式支持有限 在xlrd 1.2.0之后的版本中(大约从2020年开始)xlrd库不再支持.xlsx文件格式这限制了xlrd在新版Excel文件(主要是.xlsx格式)上的应用。
2-2、功能相对单一 xlrd库主要专注于从Excel文件中读取数据而不提供写入或修改Excel文件的功能这使得在处理需要写入或修改Excel文件的任务时用户需要结合其他库(如openpyxl或xlwt)使用。
2-3、更新和维护频率低 由于xlrd库主要关注于读取Excel文件的功能并且随着.xlsx格式的普及其使用范围逐渐缩小因此xlrd库的更新和维护频率可能相对较低。
2-4、依赖外部资源 在某些情况下xlrd库可能需要依赖外部资源或库来完全发挥其功能这可能会增加用户在使用xlrd库时的复杂性和不确定性。 总之xlrd库在读取Excel文件方面具有高效、开源和简单易用等优点但在对.xlsx格式的支持、功能单一以及更新和维护频率等方面存在一些缺点用户在选择使用xlrd库时需要根据自己的需求进行权衡和选择。 三、xlrd库的版本说明 xlrd库适配的Python版本根据库的不同版本而有所不同。以下是针对几个主要版本的说明
1、xlrd 1.2.0版本
1-1、适配Python2.7(不包括3.0-3.3)或Python3.4。 1-2、该版本支持xlsx文件格式并且是一个广泛使用的版本因为它能够处理小到中等大小的Excel文件并且具有较好的性能表现。
2、xlrd 2.0.1版本
2-1、适配Python2.7(不包括3.0-3.5)或Python3.6。 2-2、该版本不再支持xlsx文件格式仅支持旧版的xls文件格式因为在xlrd 2.0版本之后xlrd移除了对xlsx格式的支持。
3、xlrd3(非官方名称) xlrd3是xlrd的开源扩展库提供了对xlsx文件格式的支持然而请注意xlrd3并不是xlrd的官方名称(下载GitHub - Dragon2fly/xlrd3)。
四、如何学好xlrd库
1、获取xlrd库的属性和方法 用print()和dir()两个函数获取xlrd库所有属性和方法的列表
# [Book, FILE_FORMAT_DESCRIPTIONS, FMLA_TYPE_ARRAY, FMLA_TYPE_CELL, FMLA_TYPE_COND_FMT, FMLA_TYPE_DATA_VAL,
# FMLA_TYPE_NAME, FMLA_TYPE_SHARED, Operand, PEEK_SIZE, Ref3D, XLDateError, XLRDError, XLS_SIGNATURE,
# XL_CELL_BLANK, XL_CELL_BOOLEAN, XL_CELL_DATE, XL_CELL_EMPTY, XL_CELL_ERROR, XL_CELL_NUMBER, XL_CELL_TEXT, ZIP_SIGNATURE,
# __VERSION__, __builtins__, __cached__, __doc__, __file__, __loader__, __name__, __package__, __path__,
# __spec__, __version__,
# biff_text_from_num, biffh, book, cellname, cellnameabs, colname, compdoc, count_records, decompile_formula,
# dump, dump_formula, empty_cell, error_text_from_code, evaluate_name_formula, formatting, formula, info,
# inspect_format, oBOOL, oERR, oNUM, oREF, oREL, oSTRG, oUNK, okind_dict, open_workbook, open_workbook_xls,
# os, pprint, rangename3d, rangename3drel, sheet, sys, timemachine, xldate, xldate_as_datetime, xldate_as_tuple, zipfile]
2、获取xlrd库的帮助信息 用help()函数获取xlrd库的帮助信息
Help on package xlrd:NAMExlrdDESCRIPTION# Copyright (c) 2005-2012 Stephen John Machin, Lingfo Pty Ltd# This module is part of the xlrd package, which is released under a# BSD-style licence.PACKAGE CONTENTSbiffhbookcompdocformattingformulainfosheettimemachinexldateFUNCTIONScount_records(filename, outfile_io.TextIOWrapper namestdout modew encodingutf-8)For debugging and analysis: summarise the files BIFF records.ie: produce a sorted file of (record_name, count).:param filename: The path to the file to be summarised.:param outfile: An open file, to which the summary is written.dump(filename, outfile_io.TextIOWrapper namestdout modew encodingutf-8, unnumberedFalse)For debugging: dump an XLS files BIFF records in char hex.:param filename: The path to the file to be dumped.:param outfile: An open file, to which the dump is written.:param unnumbered: If true, omit offsets (for meaningful diffs).inspect_format(pathNone, contentNone)Inspect the content at the supplied path or the :class:bytes content providedand return the files type as a :class:str, or None if it cannotbe determined.:param path:A :class:string str path containing the content to inspect.~ will be expanded.:param content:The :class:bytes content to inspect.:returns:A :class:str, or None if the format cannot be determined.The return value can always be looked up in :data:FILE_FORMAT_DESCRIPTIONSto return a human-readable description of the format found.open_workbook(filenameNone, logfile_io.TextIOWrapper namestdout modew encodingutf-8, verbosity0, use_mmapTrue, file_contentsNone, encoding_overrideNone, formatting_infoFalse, on_demandFalse, ragged_rowsFalse, ignore_workbook_corruptionFalse)Open a spreadsheet file for data extraction.:param filename: The path to the spreadsheet file to be opened.:param logfile: An open file to which messages and diagnostics are written.:param verbosity: Increases the volume of trace material written to thelogfile.:param use_mmap:Whether to use the mmap module is determined heuristically.Use this arg to override the result.Current heuristic: mmap is used if it exists.:param file_contents:A string or an :class:mmap.mmap object or some other behave-alikeobject. If file_contents is supplied, filename will not be used,except (possibly) in messages.:param encoding_override:Used to overcome missing or bad codepage informationin older-version files. See :doc:unicode.:param formatting_info:The default is False, which saves memory.In this case, Blank cells, which are those with their own formattinginformation but no data, are treated as empty by ignoring the filesBLANK and MULBLANK records.This cuts off any bottom or right margin of rows of empty or blankcells.Only :meth:~xlrd.sheet.Sheet.cell_value and:meth:~xlrd.sheet.Sheet.cell_type are available.When True, formatting information will be read from the spreadsheetfile. This provides all cells, including empty and blank cells.Formatting information is available for each cell.Note that this will raise a NotImplementedError when used with anxlsx file.:param on_demand:Governs whether sheets are all loaded initially or when demandedby the caller. See :doc:on_demand.:param ragged_rows:The default of False means all rows are padded out with empty cells sothat all rows have the same size as found in:attr:~xlrd.sheet.Sheet.ncols.True means that there are no empty cells at the ends of rows.This can result in substantial memory savings if rows are of widelyvarying sizes. See also the :meth:~xlrd.sheet.Sheet.row_len method.:param ignore_workbook_corruption:This option allows to read corrupted workbooks.When False you may face CompDocError: Workbook corruption.When True that exception will be ignored.:returns: An instance of the :class:~xlrd.book.Book class.DATAFILE_FORMAT_DESCRIPTIONS {xls: Excel xls, xlsb: Excel 2007 xl...FMLA_TYPE_ARRAY 4FMLA_TYPE_CELL 1FMLA_TYPE_COND_FMT 8FMLA_TYPE_DATA_VAL 16FMLA_TYPE_NAME 32FMLA_TYPE_SHARED 2PEEK_SIZE 8XLS_SIGNATURE b\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1XL_CELL_BLANK 6XL_CELL_BOOLEAN 4XL_CELL_DATE 3XL_CELL_EMPTY 0XL_CELL_ERROR 5XL_CELL_NUMBER 2XL_CELL_TEXT 1ZIP_SIGNATURE bPK\x03\x04__VERSION__ 2.0.1biff_text_from_num {0: (not BIFF), 20: 2.0, 21: 2.1, 30: 3, ...empty_cell empty:error_text_from_code {0: #NULL!, 7: #DIV/0!, 15: #VALUE!, 23: ...oBOOL 3oERR 4oNUM 2oREF -1oREL -2oSTRG 1oUNK 0okind_dict {-2: oREL, -1: oREF, 0: oUNK, 1: oSTRG, 2: oNUM...VERSION2.0.1FILEe:\python_workspace\pythonproject\lib\site-packages\xlrd\__init__.py
3、实战案例
3-38、通过索引引用工作表
# 38、通过索引引用工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 指定要引用的工作表的索引从0开始
sheet_index 0 # 替换为你要引用的工作表的索引
# 通过索引引用工作表
try:sheet workbook.sheet_by_index(sheet_index)# 工作表对象已成功获取你可以在此进行后续操作print(f引用到的工作表名称为{sheet.name}) # 输出引用到的工作表名称为test1# 例如读取第一行的数据row_values sheet.row_values(0)print(f第一行的数据为{row_values}) # 输出第一行的数据为[45457.0, , , ]
except IndexError:print(f索引 {sheet_index} 超出范围或文件未包含工作表)
# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
3-39、通过名称引用工作表
# 39、通过名称引用工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 指定要引用的工作表的名称
sheet_name test1 # 替换为你的工作表名称
# 通过名称引用工作表
try:sheet workbook.sheet_by_name(sheet_name)# 工作表对象已成功获取你可以在此进行后续操作print(f引用到的工作表名称为{sheet.name}) # 输出引用到的工作表名称为test1# 例如读取第一行的数据row_values sheet.row_values(0)print(f第一行的数据为{row_values}) # 输出第一行的数据为[45457.0, , , ]
except xlrd.XLRDError:print(f未找到名为 {sheet_name} 的工作表)
# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
3-40、通过sheet_names集合引用工作表
# 40、通过sheet_names集合引用工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 获取所有工作表的名称
sheet_names workbook.sheet_names()
# 遍历工作表名称列表
for sheet_name in sheet_names:# 使用sheet_by_name方法获取工作表对象sheet workbook.sheet_by_name(sheet_name)# 打印工作表名称print(f工作表名称: {sheet_name}) # 输出工作表名称: test1# 在这里你可以进一步处理每个工作表比如读取数据等
# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
3-41、引用当前活动工作表
略xlrd库不支持此功能
3-42、引用第一个工作表
# 42、引用第一个工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的.xls文件路径
# 获取第一个工作表
sheet workbook.sheet_by_index(0) # 工作表索引从0开始
# 显示工作表名称在xlrd中我们不需要检查工作表类型因为sheet_by_index返回的都是工作表
print(f当前活动工作表名称为{sheet.name}) # 输出当前活动工作表名称为test1
# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
3-43、引用最后一个工作表
# 43、引用最后一个工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 获取所有工作表的名称
sheet_names workbook.sheet_names()
# 获取最后一个工作表的名称
last_sheet_name sheet_names[-1]
# 通过名称获取最后一个工作表
last_sheet workbook.sheet_by_name(last_sheet_name)
# 现在可以读取最后一个工作表中的数据了
# 例如打印出该工作表的第一行数据
first_row_values last_sheet.row_values(0)
print(最后一个工作表的第一行数据, first_row_values) # 输出最后一个工作表的第一行数据 [45457.0, , , ]
3-44、引用所有的工作表
# 44、引用所有的工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 获取所有工作表的名称
sheet_names workbook.sheet_names()
# 遍历工作表名称列表
for index, sheet_name in enumerate(sheet_names):# 通过名称引用工作表或者你也可以使用sheet_by_index(index)sheet workbook.sheet_by_name(sheet_name)# 输出工作表的名称和第一行的数据假设存在print(f工作表 {index 1}: {sheet_name})try:row_values sheet.row_values(0)print(f第一行的数据为{row_values})except IndexError:print(f工作表 {sheet_name} 是空的或者没有第一行数据)# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
# 输出
# 工作表 1: test1
# 第一行的数据为[45457.0, , , ]
# 工作表 2: test2
# 第一行的数据为[45458.0, Bryce, 6岁]
3-45、引用指定的多个工作表
# 45、引用指定的多个工作表
import xlrd
# 打开Excel文件
workbook xlrd.open_workbook(rE:\360Downloads\test.xls) # 替换为你的Excel文件路径
# 指定你想要引用的工作表名称列表
sheet_names_to_reference [test1, test2] # 替换为你要引用的工作表名称
# 遍历工作表名称列表
for sheet_name in sheet_names_to_reference:# 检查工作表名称是否存在于工作簿中if sheet_name in workbook.sheet_names():# 通过名称引用工作表 sheet workbook.sheet_by_name(sheet_name)# 输出工作表的名称和第一行的数据假设存在print(f引用到的工作表名称为{sheet_name})try:row_values sheet.row_values(0)print(f第一行的数据为{row_values})except IndexError:print(f工作表 {sheet_name} 是空的或者没有第一行数据)else:print(f工作表 {sheet_name} 不存在于工作簿中)# 不需要显式地将工作表或工作簿设置为None因为Python有垃圾回收机制
# 输出
# 引用到的工作表名称为test1
# 第一行的数据为[45457.0, , , ]
# 引用到的工作表名称为test2
# 第一行的数据为[45458.0, Bryce, 6岁]
3-46、引用新建的工作表
略xlrd库无法单独实现需结合外部其他库。
3-47、引用包含特定单元格的工作表
# 47、引用包含特定单元格的工作表
import xlrd
def find_sheet_containing_cell(file_path, cell_content):# 打开 Excel 文件workbook xlrd.open_workbook(file_path)# 遍历所有工作表for sheet in workbook.sheets():for row_idx in range(sheet.nrows):for col_idx in range(sheet.ncols):# 检查单元格内容是否匹配if sheet.cell(row_idx, col_idx).value cell_content:print(fSheet {sheet.name} contains the cell with content {cell_content} at ({row_idx}, {col_idx}))return sheet.nameprint(fNo sheet contains the cell with content {cell_content})return None
if __name__ __main__:file_path rE:\360Downloads\test.xlscell_content Bryce # 替换为你要查找的单元格内容sheet_name find_sheet_containing_cell(file_path, cell_content)if sheet_name:print(fThe sheet containing the cell is: {sheet_name})
# 输出
# Sheet test2 contains the cell with content Bryce at (0, 1)
# The sheet containing the cell is: test2
3-48、引用工作表名称中包含特定字符串的工作表
# 48、引用工作表名称中包含特定字符串的工作表
import xlrd
def find_sheets_with_name_containing(file_path, name_substring):# 打开 Excel 文件workbook xlrd.open_workbook(file_path)# 存储匹配的工作表名称matching_sheets []# 遍历所有工作表for sheet in workbook.sheets():# 检查工作表名称是否包含特定字符串if name_substring in sheet.name:print(fSheet {sheet.name} contains the substring {name_substring})matching_sheets.append(sheet.name)if not matching_sheets:print(fNo sheets contain the substring {name_substring} in their names.)return matching_sheets
if __name__ __main__:file_path rE:\360Downloads\test.xlsname_substring test2 # 替换为你要查找的字符串matching_sheets find_sheets_with_name_containing(file_path, name_substring)if matching_sheets:print(fThe sheets containing the substring are: {matching_sheets})
# 输出
# Sheet test2 contains the substring test2
# The sheets containing the substring are: [test2]
五、推荐阅读
1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页