赞
踩
目录
3-85、利用类似Resize属性引用变更为指定大小的单元格区域
在Excel中,通常所说的“情侣键”并非官方术语,而是对某些常用且经常成对出现的快捷键的一种形象化的称呼。其中,最为人熟知和广泛使用的“情侣键”是“Ctrl+C”和“Ctrl+V”。
1、Ctrl+C:这个快捷键的作用是“拷贝”或“复制”。当你在Excel中选中某个单元格、一行、一列或整个工作表的内容后,按下Ctrl+C键,这些内容就会被复制到计算机的剪贴板中,等待下一步的粘贴操作。
2、Ctrl+V:这个快捷键的作用是“粘贴”。在你按下Ctrl+C键将内容复制到剪贴板后,可以通过按下Ctrl+V键将这些内容粘贴到Excel中的另一个位置,这两个操作经常是连续进行的,因此Ctrl+C和Ctrl+V就像一对“情侣”,总是成对出现。
除了这对常见的“情侣键”外,Excel中还有许多其他的快捷键可以帮助用户更高效地完成各种操作。然而,这些快捷键通常并没有像Ctrl+C和Ctrl+V那样形成特定的“情侣”关系。
然而,今天我不再展开介绍“情侣键”,而是要重点推介Python中的“情侣库”,即xlrd和xlwt两个第三方库。
xlrd库是一种用于在Python中读取Excel文件的库,它的名称中的"xl"代表Excel,"rd"代表读取,其开发者是John Machin(注:库名字符拆分诠释,只是一种猜测)。
xlrd最初是在2005年开始开发的,是基于Python的开源项目(下载:xlrd库官网下载)。
由于Excel文件在数据处理和分析中的重要性,xlrd库填补了Python在处理Excel文件方面的空白,使得用户可以方便地在Python环境中读取Excel文件的内容,并进行进一步的数据操作和分析。
xlrd库支持多种Excel文件格式,包括`.xls`和`.xlsx`(在旧版本中),这使得无论数据存储在哪种格式的Excel文件中,用户都可以使用xlrd库来读取。
xlrd库使用C语言编写,因此其性能非常高,即使面对非常大的Excel文件,xlrd也可以快速地读取其中的数据。
xlrd是完全开源的,可以在GitHub等平台上找到其源代码,这使得任何人都可以根据自己的需求对其进行修改和扩展。
xlrd提供了简单直接的API来获取单元格数据、行列数等,使得从Excel文件中读取数据变得简单而高效。
xlrd库适配多种Python版本,包括Python 2.7(不包括3.0-3.3)或Python 3.4及以上版本,这为用户提供了广泛的兼容性选择。
在xlrd 1.2.0之后的版本中(大约从2020年开始),xlrd库不再支持`.xlsx`文件格式,这限制了xlrd在新版Excel文件(主要是`.xlsx`格式)上的应用。
xlrd库主要专注于从Excel文件中读取数据,而不提供写入或修改Excel文件的功能,这使得在处理需要写入或修改Excel文件的任务时,用户需要结合其他库(如`openpyxl`或`xlwt`)使用。
由于xlrd库主要关注于读取Excel文件的功能,并且随着`.xlsx`格式的普及,其使用范围逐渐缩小,因此,xlrd库的更新和维护频率可能相对较低。
在某些情况下,xlrd库可能需要依赖外部资源或库来完全发挥其功能,这可能会增加用户在使用xlrd库时的复杂性和不确定性。
总之,xlrd库在读取Excel文件方面具有高效、开源和简单易用等优点,但在对`.xlsx`格式的支持、功能单一以及更新和维护频率等方面存在一些缺点,用户在选择使用xlrd库时需要根据自己的需求进行权衡和选择。
xlrd库适配的Python版本根据库的不同版本而有所不同。以下是针对几个主要版本的说明:
1-1、适配Python>=2.7(不包括3.0-3.3)或Python>=3.4。
1-2、该版本支持xlsx文件格式,并且是一个广泛使用的版本,因为它能够处理小到中等大小的Excel文件,并且具有较好的性能表现。
2-1、适配Python>=2.7(不包括3.0-3.5)或Python>=3.6。
2-2、该版本不再支持xlsx文件格式,仅支持旧版的xls文件格式,因为在xlrd 2.0版本之后,xlrd移除了对xlsx格式的支持。
xlrd3是xlrd的开源扩展库,提供了对xlsx文件格式的支持,然而,请注意,xlrd3并不是xlrd的官方名称(下载:GitHub - Dragon2fly/xlrd3)。
用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']
用help()函数获取xlrd库的帮助信息
- Help on package xlrd:
-
- NAME
- xlrd
-
- DESCRIPTION
- # 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 CONTENTS
- biffh
- book
- compdoc
- formatting
- formula
- info
- sheet
- timemachine
- xldate
-
- FUNCTIONS
- count_records(filename, outfile=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)
- For debugging and analysis: summarise the file's 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 name='<stdout>' mode='w' encoding='utf-8'>, unnumbered=False)
- For debugging: dump an XLS file's 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(path=None, content=None)
- Inspect the content at the supplied path or the :class:`bytes` content provided
- and return the file's type as a :class:`str`, or ``None`` if it cannot
- be 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_DESCRIPTIONS`
- to return a human-readable description of the format found.
-
- open_workbook(filename=None, logfile=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, verbosity=0, use_mmap=True, file_contents=None, encoding_override=None, formatting_info=False, on_demand=False, ragged_rows=False, ignore_workbook_corruption=False)
- 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 the
- logfile.
-
- :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-alike
- object. If ``file_contents`` is supplied, ``filename`` will not be used,
- except (possibly) in messages.
-
- :param encoding_override:
-
- Used to overcome missing or bad codepage information
- in 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 formatting
- information but no data, are treated as empty by ignoring the file's
- ``BLANK`` and ``MULBLANK`` records.
- This cuts off any bottom or right "margin" of rows of empty or blank
- cells.
- 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 spreadsheet
- file. 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 an
- xlsx file.
-
- :param on_demand:
-
- Governs whether sheets are all loaded initially or when demanded
- by the caller. See :doc:`on_demand`.
-
- :param ragged_rows:
-
- The default of ``False`` means all rows are padded out with empty cells so
- that 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 widely
- varying 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.
-
- DATA
- FILE_FORMAT_DESCRIPTIONS = {'xls': 'Excel xls', 'xlsb': 'Excel 2007 xl...
- FMLA_TYPE_ARRAY = 4
- FMLA_TYPE_CELL = 1
- FMLA_TYPE_COND_FMT = 8
- FMLA_TYPE_DATA_VAL = 16
- FMLA_TYPE_NAME = 32
- FMLA_TYPE_SHARED = 2
- PEEK_SIZE = 8
- XLS_SIGNATURE = b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'
- XL_CELL_BLANK = 6
- XL_CELL_BOOLEAN = 4
- XL_CELL_DATE = 3
- XL_CELL_EMPTY = 0
- XL_CELL_ERROR = 5
- XL_CELL_NUMBER = 2
- XL_CELL_TEXT = 1
- ZIP_SIGNATURE = b'PK\x03\x04'
- __VERSION__ = '2.0.1'
- biff_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 = 3
- oERR = 4
- oNUM = 2
- oREF = -1
- oREL = -2
- oSTRG = 1
- oUNK = 0
- okind_dict = {-2: 'oREL', -1: 'oREF', 0: 'oUNK', 1: 'oSTRG', 2: 'oNUM'...
-
- VERSION
- 2.0.1
-
- FILE
- e:\python_workspace\pythonproject\lib\site-packages\xlrd\__init__.py
略,xlrd库不支持此功能,需要借助其他库实现
- # 74、引用单元格的快捷方式
- import xlrd
- def excel_to_rowcol(excel_ref):
- """
- 将 Excel 单元格引用(如 "A1")转换为行号和列索引(从 0 开始)
- """
- col = ''.join(filter(str.isalpha, excel_ref))
- row_str = ''.join(filter(str.isdigit, excel_ref))
- row = int(row_str) - 1 # Excel 中的行号从 1 开始,Python 从 0 开始
- col_idx = 0
- for c in col:
- col_idx = col_idx * 26 + (ord(c.upper()) - ord('A') + 1)
- return row, col_idx - 1 # 列索引也从 0 开始
- if __name__ == '__main__':
- workbook = xlrd.open_workbook('example.xls')
- sheet = workbook.sheet_by_index(0)
- # 假设要引用的单元格是 "B2"
- cell_ref = "B2"
- row, col = excel_to_rowcol(cell_ref)
- # 使用 xlrd 读取单元格数据
- cell_value = sheet.cell_value(row, col)
- print(cell_value)
- # 75、引用某个单元格
- import xlrd
- # 打开工作簿
- workbook = xlrd.open_workbook('example.xls')
- # 选择工作表(例如,选择第一个工作表)
- sheet = workbook.sheet_by_index(0)
- # 指定要读取的单元格的行号和列号
- # 假设我们要读取第3行第2列(即Excel中的C3单元格)
- row_index = 2 # 行号从0开始,所以第3行是索引2
- col_index = 1 # 列号从0开始,所以第2列是索引1
- # 读取指定单元格的数据
- cell_value = sheet.cell_value(row_index, col_index)
- # 打印单元格的值
- print("单元格C3的值是:", cell_value) # 输出:单元格C3的值是: 45458.0
- # 76、引用工作表的全部单元格
- import xlrd
- # 打开工作簿
- workbook = xlrd.open_workbook('example.xls')
- # 选择工作表(例如,选择第一个工作表)
- sheet = workbook.sheet_by_index(0)
- # 遍历工作表的每一行
- for row_idx in range(sheet.nrows):
- # 初始化一个列表来存储当前行的所有单元格数据
- row_data = []
- # 遍历当前行的每一列
- for col_idx in range(sheet.ncols):
- # 读取单元格的值并添加到当前行的列表中
- cell_value = sheet.cell_value(row_idx, col_idx)
- row_data.append(cell_value)
- # 这里可以处理整行的数据,例如打印它
- print(row_data)
- # 如果你只想获取所有单元格的值,而不需要处理每一行,可以将它们存储在一个二维列表中
- all_cell_values = []
- for row_idx in range(sheet.nrows):
- row_data = []
- for col_idx in range(sheet.ncols):
- cell_value = sheet.cell_value(row_idx, col_idx)
- row_data.append(cell_value)
- all_cell_values.append(row_data)
- # 现在all_cell_values包含了工作表中所有单元格的值
- # 你可以根据需要进一步处理这个二维列表
- # 77、引用连续的单元格区域
- import xlrd
- # 打开工作簿
- workbook = xlrd.open_workbook('example.xls')
- # 选择工作表(例如,选择第一个工作表)
- sheet = workbook.sheet_by_index(0)
- # 定义连续单元格区域的起始行、起始列、结束行和结束列
- start_row = 1 # Excel中的第2行
- start_col = 0 # Excel中的第1列(A列)
- end_row = 5 # Excel中的第6行
- end_col = 2 # Excel中的第3列(C列)
- # 遍历连续单元格区域
- for row_idx in range(start_row - 1, end_row): # Python的索引从0开始,所以需要减1
- row_data = []
- for col_idx in range(start_col, end_col + 1): # 加上1以确保包含结束列
- cell_value = sheet.cell_value(row_idx, col_idx)
- row_data.append(cell_value)
- # 这里可以处理每一行的数据,例如打印它
- print(row_data)
- # 78、通过输入框(tkinter)引用所选择的单元格
- import tkinter as tk
- from tkinter import simpledialog
- import xlrd
- def read_cell_from_excel(file_path, cell_ref):
- try:
- workbook = xlrd.open_workbook(file_path)
- sheet = workbook.sheet_by_index(0) # 假设读取第一个工作表
- row_idx, col_idx = excel_to_rowcol(cell_ref)
- cell_value = sheet.cell_value(row_idx, col_idx)
- return cell_value
- except Exception as e:
- return str(e)
- def excel_to_rowcol(excel_ref):
- """
- 将 Excel 单元格引用(如 "A1")转换为行号和列索引(从 0 开始)。
- """
- col, row_str = excel_ref.upper().split()
- row = int(row_str) - 1
- col_idx = 0
- for c in col:
- col_idx = col_idx * 26 + (ord(c) - ord('A') + 1)
- return row, col_idx - 1
- def on_select_file():
- file_path = simpledialog.askopenfilename(filetypes=[("Excel files", "*.xls;*.xlsx")])
- if file_path:
- cell_ref = input_box.get() # 获取输入框中的单元格引用
- cell_value = read_cell_from_excel(file_path, cell_ref)
- result_label.config(text=cell_value)
- root = tk.Tk()
- root.title("Excel Cell Reader")
- # 创建输入框用于输入单元格引用
- input_box = tk.Entry(root)
- input_box.pack(pady=10)
- # 创建按钮用于选择文件并读取单元格
- select_button = tk.Button(root, text="Select File and Read Cell", command=on_select_file)
- select_button.pack(pady=10)
- # 创建标签用于显示结果
- result_label = tk.Label(root, text="")
- result_label.pack(pady=10)
- root.mainloop()
- # 79、引用活动单元格
- import xlrd
- def excel_to_rowcol(excel_ref):
- """
- 将 Excel 单元格引用(如 "A1")转换为行号和列索引(从 0 开始)
- """
- col = ''.join(filter(str.isalpha, excel_ref))
- row_str = ''.join(filter(str.isdigit, excel_ref))
- row = int(row_str) - 1 # Excel 中的行号从 1 开始,Python 从 0 开始
- col_idx = 0
- for c in col:
- col_idx = col_idx * 26 + (ord(c.upper()) - ord('A') + 1)
- return row, col_idx - 1 # 列索引也从 0 开始
- # 示例用法
- # 打开工作簿
- workbook = xlrd.open_workbook('example.xls')
- # 假设活动工作表是第一个工作表
- sheet = workbook.sheet_by_index(0)
- # 获取活动单元格引用 (例如 "B2")
- # 你需要自己定义如何获取这个活动单元格的引用,这里假设它是已知的
- cell_ref = "B2"
- # 将单元格引用转换为行号和列索引
- row, col = excel_to_rowcol(cell_ref)
- # 使用 xlrd 读取单元格数据
- cell_value = sheet.cell_value(row, col)
- print(f'活动单元格 {cell_ref} 的值是: {cell_value}') # 输出:活动单元格 B2 的值是: 45458.0
- # 80、引用已选定的单元格
- import xlrd
- # 打开Excel文件
- workbook = xlrd.open_workbook('example.xls')
- # 选择第一个工作表
- sheet = workbook.sheet_by_index(0)
- # 读取特定单元格的值
- # 假设我们要读取第一行第一列的单元格(即A1单元格)
- cell_value = sheet.cell(0, 0).value
- print(f"The value of the selected cell is: {cell_value}") # 输出:The value of the selected cell is: 45458.0
- # 81、引用已使用的单元格区域
- import xlrd
- # 打开Excel文件
- workbook = xlrd.open_workbook('example.xls')
- # 选取特定的工作表,例如第一个工作表
- worksheet = workbook.sheet_by_index(0)
- # 获取工作表的有效行数和列数
- rows = worksheet.nrows
- cols = worksheet.ncols
- # 遍历并打印每个已使用单元的数据
- for row_index in range(rows):
- for col_index in range(cols):
- # 获取单元格的值
- cell_value = worksheet.cell(row_index, col_index).value
- print(f'Row {row_index}, Column {col_index}: {cell_value}')
- # 82、引用被空白行和列包围的单元格区域
- import xlrd
- # 打开Excel文件
- workbook = xlrd.open_workbook('example.xls')
- # 选取特定的工作表,例如第一个工作表
- worksheet = workbook.sheet_by_index(0)
- # 找到包含数据的最小和最大行、列索引
- min_row, max_row = None, None
- min_col, max_col = None, None
- for row_index in range(worksheet.nrows):
- for col_index in range(worksheet.ncols):
- cell_value = worksheet.cell(row_index, col_index).value
- if cell_value != "":
- if min_row is None or row_index < min_row:
- min_row = row_index
- if max_row is None or row_index > max_row:
- max_row = row_index
- if min_col is None or col_index < min_col:
- min_col = col_index
- if max_col is None or col_index > max_col:
- max_col = col_index
- # 打印包含数据的单元格区域
- if min_row is not None and min_col is not None:
- for row_index in range(min_row, max_row + 1):
- for col_index in range(min_col, max_col + 1):
- cell_value = worksheet.cell(row_index, col_index).value
- print(f'Row {row_index}, Column {col_index}: {cell_value}')
- else:
- print("The worksheet is empty.")
- # 83、利用类似Offset属性动态引用某个单元格
- import xlrd
- # 打开Excel文件
- workbook = xlrd.open_workbook('example.xls')
- # 选取特定的工作表,例如第一个工作表
- worksheet = workbook.sheet_by_index(0)
- # 定义起始单元格的位置,例如A1
- start_row = 0 # 对应Excel中的第1行
- start_col = 0 # 对应Excel中的第1列
- # 定义偏移量,例如偏移2行3列
- offset_row = 2
- offset_col = 3
- # 计算目标单元格的位置
- target_row = start_row + offset_row
- target_col = start_col + offset_col
- # 检查目标单元格是否在工作表范围内
- if target_row < worksheet.nrows and target_col < worksheet.ncols:
- # 获取目标单元格的值
- cell_value = worksheet.cell(target_row, target_col).value
- print(f'Target cell value at ({target_row+1}, {target_col+1}): {cell_value}') # 输出:Target cell value at (3, 4): Myelsa
- else:
- print("The target cell is out of the worksheet range.")
- # 84、利用类似Offset属性动态引用单元格区域
- import xlrd
- # 打开工著名的 Excel 文件
- workbook = xlrd.open_workbook('example.xls')
- worksheet = workbook.sheet_by_index(0) # 选择工作表,这里选择第一张表
- # 定位起始单元格(例如第二行第三列,即B3)
- start_row = 1 # Excel 是从 1 开始计数,但在Python中列表从0开始
- start_col = 2
- # 定义要引用的区域大小,如 4 行 5 列
- num_rows = 4
- num_cols = 5
- # 使用循环结构读取单元格区域内的数据
- for r in range(start_row, start_row + num_rows):
- for c in range(start_col, start_col + num_cols):
- # 检查是否超出表格范围
- if r < worksheet.nrows and c < worksheet.ncols:
- # 获取单元格内容
- cell_value = worksheet.cell(r, c).value
- print(f'({r+1}, {c+1}): {cell_value}')
- else:
- # 跳过此单元格,防止越界
- print('Cell out of range.')
- # 请确保example.xlsx文件在脚本可以访问的路径中,并且存在对应的数据
- # 85、利用类似Resize属性引用变更为指定大小的单元格区域
- import xlrd
- def get_resized_area(sheet, start_row, start_col, num_rows, num_cols):
- # 准备容器以存放单元格数据
- data = []
- for r in range(start_row, start_row + num_rows):
- row_data = []
- for c in range(start_col, start_col + num_cols):
- # 检查是否超出工作表范围
- if r < sheet.nrows and c < sheet.ncols:
- # 获取并添加单元格内容
- cell_value = sheet.cell_value(r, c)
- row_data.append(cell_value)
- else:
- # 如果超出范围则添加空值或者自定义的填充值
- row_data.append(None)
- data.append(row_data)
- return data
- # 使用例子
- workbook = xlrd.open_workbook('example.xls')
- worksheet = workbook.sheet_by_name('test1')
- # 设定起始单元格和区域大小
- start_row_index = 1 # Excel中的B3对应于row index 1
- start_col_index = 2 # Excel中的B3对应于col index 2
- num_rows_to_resize = 4
- num_cols_to_resize = 3
- # 获取调整大小后的区域
- resized_area = get_resized_area(worksheet, start_row_index, start_col_index, num_rows_to_resize, num_cols_to_resize)
- # 打印结果来验证
- for row in resized_area:
- print(row)
- # 86、引用不包括标题行的单元格区域
- import xlrd
- def get_resized_area_excluding_headers(sheet, start_row, start_col, num_rows, num_cols):
- # 准备容器以存放单元格数据
- data = []
- for r in range(start_row + 1, start_row + 1 + num_rows): # 跳过标题行
- row_data = []
- for c in range(start_col, start_col + num_cols):
- # 检查是否超出工作表范围
- if r < sheet.nrows and c < sheet.ncols:
- # 获取并添加单元格内容
- cell_value = sheet.cell_value(r, c)
- row_data.append(cell_value)
- else:
- # 如果超出范围则添加空值或者自定义的填充值
- row_data.append(None)
- data.append(row_data)
- return data
- # 使用例子
- workbook = xlrd.open_workbook('example.xls')
- worksheet = workbook.sheet_by_name('test1')
- # 设定起始单元格和区域大小
- start_row_index = 0 # 标题行通常是第0行
- start_col_index = 0 # 根据需要调整列的起始位置
- num_rows_to_resize = 4 # 需要引用的行数
- num_cols_to_resize = 3 # 需要引用的列数
- # 获取调整大小后的区域,排除标题行
- resized_area = get_resized_area_excluding_headers(worksheet, start_row_index, start_col_index, num_rows_to_resize, num_cols_to_resize)
- # 打印结果来验证
- for row in resized_area:
- print(row)
- # 87、引用不包括标题列的单元格区域
- import xlrd
- def get_resized_area_excluding_headers(sheet, start_row, start_col, num_rows, num_cols):
- # 准备容器以存放单元格数据
- data = []
- for r in range(start_row, start_row + num_rows):
- row_data = []
- for c in range(start_col + 1, start_col + 1 + num_cols): # 跳过标题列
- # 检查是否超出工作表范围
- if r < sheet.nrows and c < sheet.ncols:
- # 获取并添加单元格内容
- cell_value = sheet.cell_value(r, c)
- row_data.append(cell_value)
- else:
- # 如果超出范围则添加空值或者自定义的填充值
- row_data.append(None)
- data.append(row_data)
- return data
- # 使用例子
- workbook = xlrd.open_workbook('example.xls')
- worksheet = workbook.sheet_by_name('test1')
- # 设定起始单元格和区域大小
- start_row_index = 0 # 根据需要调整行的起始位置
- start_col_index = 0 # 标题列通常是第0列
- num_rows_to_resize = 4 # 需要引用的行数
- num_cols_to_resize = 3 # 需要引用的列数
- # 获取调整大小后的区域,排除标题列
- resized_area = get_resized_area_excluding_headers(worksheet, start_row_index, start_col_index, num_rows_to_resize, num_cols_to_resize)
- # 打印结果来验证
- for row in resized_area:
- print(row)
- # 88、引用任意单元格区域的右下角单元格
- import xlrd
- # 打开工作簿(Workbook)
- workbook = xlrd.open_workbook('example.xls')
- # 通过索引或名称选择工作表(Sheet)
- worksheet = workbook.sheet_by_name('test1')
- # 设定单元格区域的左上角起点坐标(行索引,列索引)
- start_row = 1 # 比如第2行(索引从0开始)
- start_col = 1 # 比如第2列(索引从0开始)
- # 区域的高度(行数)和宽度(列数)
- num_rows = 5 # 区域高度为5
- num_cols = 3 # 区域宽度为3
- # 计算右下角单元格的坐标
- end_row = start_row + num_rows - 1 # 确保区域是正确的高度
- end_col = start_col + num_cols - 1 # 确保区域是正确的宽度
- # 确保给定的索引未超出工作表的最大行数和列数
- if end_row >= worksheet.nrows or end_col >= worksheet.ncols:
- print("The specified area goes beyond the worksheet's boundaries.")
- else:
- # 获取右下角单元格的值
- bottom_right_cell_value = worksheet.cell_value(end_row, end_col)
- # 输出右下角单元格的值
- print(f"The value in the bottom-right cell ({end_row + 1},{end_col + 1}) of the specified area is: {bottom_right_cell_value}")
略,xlrd库不支持此功能,需要借助其他库实现
- # 90、引用输入了常量的所有单元格
- import xlrd
- def find_cells_with_constant(file_path, constant):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历所有行和列
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell_value = sheet.cell_value(row_index, col_index)
- # 检查单元格是否包含常量
- if cell_value == constant:
- cell_location = (sheet.name, row_index, col_index)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls'
- constant = 168 # 你要查找的常量值
- result = find_cells_with_constant(file_path, constant)
- if result:
- print(f"引用常量 {constant} 的单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}")
- else:
- print(f"没有找到引用常量 {constant} 的单元格!")
- # 91、引用输入了数字的所有单元格
- import xlrd
- def find_cells_with_numbers(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历所有行和列
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell_value = sheet.cell_value(row_index, col_index)
- # 检查单元格是否包含数字
- if isinstance(cell_value, (int, float)):
- cell_location = (sheet.name, row_index, col_index, cell_value)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls'
- result = find_cells_with_numbers(file_path)
- if result:
- print(f"引用数字的单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}, 数值: {loc[3]}")
- else:
- print(f"没有找到引用数字的单元格!")
- # 92、引用输入了文本的所有单元格
- import xlrd
- def find_cells_with_text(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历所有行和列
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell_value = sheet.cell_value(row_index, col_index)
- # 检查单元格是否包含文本
- if isinstance(cell_value, str):
- cell_location = (sheet.name, row_index, col_index, cell_value)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls' # 请确保文件路径和文件名正确
- result = find_cells_with_text(file_path)
- if result:
- print("引用文本的单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}, 文本: {loc[3]}")
- else:
- print("没有找到引用文本的单元格!")
- # 93、引用输入了逻辑值的所有单元格
- import xlrd
- def find_cells_with_boolean(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历所有行和列
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell_value = sheet.cell_value(row_index, col_index)
- cell_type = sheet.cell_type(row_index, col_index)
- # 检查单元格是否包含布尔值
- if cell_type == xlrd.XL_CELL_BOOLEAN:
- cell_location = (sheet.name, row_index, col_index, cell_value)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls' # 请确保文件路径和文件名正确
- result = find_cells_with_boolean(file_path)
- if result:
- print("引用逻辑值的单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}, 逻辑值: {loc[3]}")
- else:
- print("没有找到引用逻辑值的单元格!")
- # 94、引用输入了批注的所有单元格
- import xlrd
- def find_cells_with_comments(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path, formatting_info=True)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 获取sheet的批注
- comments = sheet.cell_note_map
- # 遍历批注并获取相应的单元格
- for (row_index, col_index), note in comments.items():
- cell_location = (sheet.name, row_index, col_index, note.text)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls' # 请确保文件路径和文件名正确
- result = find_cells_with_comments(file_path)
- if result:
- print("引用批注的单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}, 批注: {loc[3]}")
- else:
- print("没有找到引用批注的单元格!")
- # 95、引用没有输入任何数据和公式的空单元格
- import xlrd
- def find_empty_cells(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path, formatting_info=True)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历sheet中的所有单元格
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell = sheet.cell(row_index, col_index)
- if cell.ctype == xlrd.XL_CELL_EMPTY:
- cell_location = (sheet.name, row_index, col_index)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls' # 请确保文件路径和文件名正确
- result = find_empty_cells(file_path)
- if result:
- print("空单元格位置如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}")
- else:
- print("没有找到空单元格!")
- # 96、引用所有可见单元格
- import xlrd
- def get_visible_cells(file_path):
- # 打开Excel文件
- workbook = xlrd.open_workbook(file_path, formatting_info=True)
- result = []
- # 遍历所有sheet
- for sheet_index in range(workbook.nsheets):
- sheet = workbook.sheet_by_index(sheet_index)
- # 遍历sheet中的所有单元格
- for row_index in range(sheet.nrows):
- for col_index in range(sheet.ncols):
- cell = sheet.cell(row_index, col_index)
- if cell.ctype != xlrd.XL_CELL_EMPTY:
- cell_location = (sheet.name, row_index, col_index, cell.value)
- result.append(cell_location)
- return result
- if __name__ == '__main__':
- file_path = 'example.xls' # 请确保文件路径和文件名正确
- result = get_visible_cells(file_path)
- if result:
- print("可见单元格位置及内容如下:")
- for loc in result:
- print(f"Sheet: {loc[0]}, 行: {loc[1] + 1}, 列: {loc[2] + 1}, 内容: {loc[3]}")
- else:
- print("没有找到可见单元格!")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。