发布于2026-07-05 阅读(0)
扫一扫,手机访问
日常处理 Excel 数据时,总会碰到一些“大块头”文件——几十万行甚至上百万行的表格,常规工具一打开就卡死。这时候 calamine 库就是一个相当趁手的轻量级方案。它支持 .xls、.xlsx、.ods 和 .csv 这几种主流格式,API 简洁到几乎不用查文档就能上手。更重要的是,它的解析速度在同类库中几乎是碾压级的,尤其适合那些不想为了一个文件就拖进整个 Pandas 生态的场景。

安装没什么悬念,一行命令的事:
pip3 install calamine
先看最常见的情况——读一个 Excel 工作簿,把数据取出来。代码结构非常直观:
from calamine import Workbook, CellType
# 打开工作簿
workbook = Workbook.open("example.xlsx")
# 获取所有的工作表名称
sheet_names = workbook.sheet_names()
print(f"Sheet names: {sheet_names}")
# 选择第一个工作表
sheet = workbook.get_sheet_by_index(0)
# 遍历所有行
for row in sheet.rows():
# 每一行是一个列表,包含每个单元格的值
print([cell.value for cell in row if cell.type != CellType.EMPTY])
# 关闭工作簿
workbook.close()
注意这里默认会跳过空单元格,只输出有值的部分,这一点在实际数据清理中很实用。
如果你的数据源是 LibreOffice 或 OpenOffice 生成的 ODS 文件,操作方式完全一致:
from calamine import Workbook, CellType
# 打开 ODS 工作簿
workbook = Workbook.open("example.ods")
# 选择第一个工作表
sheet = workbook.get_sheet_by_index(0)
# 遍历所有行
for row in sheet.rows():
print([cell.value for cell in row if cell.type != CellType.EMPTY])
# 关闭工作簿
workbook.close()
CSV 虽然不算严格意义上的电子表格,但 calamine 也给它安排了专门的入口:
from calamine import Workbook, CellType
# 打开 CSV 文件
with open("example.csv", "r") as f:
workbook = Workbook.from_csv(f)
# 选择唯一的工作表
sheet = workbook.get_sheet_by_index(0)
# 遍历所有行
for row in sheet.rows():
print([cell.value for cell in row if cell.type != CellType.EMPTY])
不同单元格可能存着不同类型的数据——字符串、数字、布尔值,甚至公式和错误值。calamine 提供了一套枚举来区分它们,让你能按需处理:
from calamine import CellType
# 假设我们已经打开了一个工作簿并选择了某个工作表
for row in sheet.rows():
for cell in row:
if cell.type == CellType.STRING:
print(f"String value: {cell.value}")
elif cell.type == CellType.NUMBER:
print(f"Number value: {cell.value}")
elif cell.type == CellType.BOOL:
print(f"Boolean value: {cell.value}")
elif cell.type == CellType.ERROR:
print(f"Error value: {cell.error}")
elif cell.type == CellType.FORMULA:
print(f"Formula: {cell.formula}, Result: {cell.value}")
elif cell.type == CellType.EMPTY:
print("Empty cell")
这种细粒度控制在大批量数据清洗时很关键——比如你想只提取数值列,或者标记公式结果。
要是你清楚具体的位置(比如 A1),直接取值就行,省去遍历:
# 获取 A1 单元格的值
value = sheet.get_value("A1")
print(f"Value at A1: {value}")
calamine 还提供了一些进阶能力,虽然不算面面俱到,但在日常场景里足够用了:
下面给一个完整的实战函数,把读取流程封装起来,顺便做个简单数据处理:
from calamine import Workbook, CellType
def read_excel(file_path):
workbook = Workbook.open(file_path)
sheet = workbook.get_sheet_by_index(0)
data = []
for row in sheet.rows():
row_data = [cell.value for cell in row if cell.type != CellType.EMPTY]
if row_data:
data.append(row_data)
workbook.close()
return data
if __name__ == "__main__":
file_path = "example.xlsx"
data = read_excel(file_path)
# 打印前几行数据作为示例
for row in data[:5]:
print(row)
为什么选择 Calamine?
跟 openpyxl 等传统库比,python-calamine 在处理大规模数据时的优势几乎是降维打击。下面这张表是 50 万行数据测试的典型结果:
| 库/引擎 | 读取 50 万行数据的耗时 | 性能对比 |
|---|---|---|
| Calamine | 约 3.58 秒 | 基准 (最快) |
| Pandas + Calamine | 约 7 - 9.4 秒 | 约 2-3 倍于纯 Calamine |
| openpyxl | 约 2 分钟 | 约 10 倍于 Calamine |
注意:不同测试场景下的数据会略有差异,但 Calamine 的领先地位非常明确。
如何安装与使用
1. 安装
通过 pip 即可轻松安装:
pip install python-calamine
如果你的项目主要使用 Pandas,只需安装 python-calamine 库,Pandas 即可自动识别并使用它。
2. 基础用法:直接使用 CalamineWorkbook
这是最直接的方式,适合对性能有极致要求,或不想引入 Pandas 依赖的场景。
from python_calamine import CalamineWorkbook
# 加载 Excel 文件
workbook = CalamineWorkbook.from_path("your_large_file.xlsx")
# 获取所有工作表名称
sheet_names = workbook.sheet_names
print(sheet_names)
# 按名称获取工作表,并将其转换为 Python 列表
sheet_data = workbook.get_sheet_by_name("Sheet1").to_python()
# 遍历数据
for row in sheet_data:
print(row)
关键点:to_python() 方法默认会跳过数据区域前后的空行和空列,提高效率。
3. 进阶用法:与 Pandas 结合(推荐)
如果你习惯使用 Pandas 进行数据分析,最便捷的方式是在 pd.read_excel() 中指定 engine="calamine"。
import pandas as pd
# 方法一:读取文件路径
df = pd.read_excel("your_large_file.xlsx", engine="calamine")
# 方法二:读取文件对象(推荐,可避免路径问题)
with open("your_large_file.xlsx", "rb") as f:
df = pd.read_excel(f, sheet_name="Sheet1", engine="calamine")
还可以结合 usecols 参数,只读取需要的列,进一步提升性能:
# 只读取 A 到 N 列
df = pd.read_excel("large_file.xlsx", usecols="A:N", engine="calamine")
calamine 是一个非常轻便且高效的工具,适用于需要快速解析多种格式电子表格的应用程序。它提供的 API 简单直观,易于集成到现有项目中。10倍性能,待验证。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8