Python讀取xlsx文件的所有Python庫大全(附代碼)
文件說明
xlsx_readers_complete.py - 完整版本,包含所有庫的詳細(xì)示例
simple_examples.py - 簡化版本,只包含核心代碼
requirements.txt - 所有依賴庫列表
README.md - 本說明文檔
庫分類和推薦
最推薦的庫
| 庫名 | 特點 | 適用場景 |
|---|---|---|
| pandas | 功能最強(qiáng)大,生態(tài)最完善 | 數(shù)據(jù)分析、數(shù)據(jù)科學(xué)項目 |
| openpyxl | 專門處理Excel,功能全面 | 純Excel文件處理 |
| polars | 高性能,內(nèi)存效率高 | 大數(shù)據(jù)處理 |
專業(yè)庫
| 庫名 | 特點 | 適用場景 |
|---|---|---|
| xlwings | 與Excel應(yīng)用程序交互 | 需要Excel高級功能 |
| pyexcel | 統(tǒng)一接口,支持多種格式 | 多格式文件處理 |
| tablib | 數(shù)據(jù)導(dǎo)入導(dǎo)出 | 數(shù)據(jù)轉(zhuǎn)換和導(dǎo)出 |
高性能庫
| 庫名 | 特點 | 適用場景 |
|---|---|---|
| fastexcel | 基于Rust,速度快 | 大文件快速讀取 |
| calamine | Rust實現(xiàn),高性能 | 性能要求極高的場景 |
| dask | 并行計算 | 超大文件處理 |
| modin | 加速pandas | pandas性能優(yōu)化 |
| vaex | 大數(shù)據(jù)可視化 | 億級數(shù)據(jù)處理 |
傳統(tǒng)庫
| 庫名 | 特點 | 適用場景 |
|---|---|---|
| xlrd | 傳統(tǒng)Excel讀取庫 | 老項目維護(hù)(注意版本兼容性) |
| xlutils | Excel工具集 | 配合xlrd使用 |
快速開始
最簡單的方法(推薦)
import pandas as pd
# 讀取xlsx文件每一行為列表
df = pd.read_excel('your_file.xlsx')
rows = df.values.tolist() # 不包含列名
# 或者包含列名:
rows_with_headers = [df.columns.tolist()] + df.values.tolist()
不依賴pandas的方法
from openpyxl import load_workbook
workbook = load_workbook('your_file.xlsx')
sheet = workbook.active
rows = [list(row) for row in sheet.iter_rows(values_only=True)]
自動選擇庫的方法
def read_xlsx_auto(filename):
"""自動選擇可用的庫來讀取xlsx文件"""
try:
import pandas as pd
df = pd.read_excel(filename)
return [df.columns.tolist()] + df.values.tolist()
except ImportError:
from openpyxl import load_workbook
workbook = load_workbook(filename)
sheet = workbook.active
return [list(row) for row in sheet.iter_rows(values_only=True)]
安裝依賴
最小安裝(適合大多數(shù)場景)
pip install pandas openpyxl
完整安裝(所有庫)
pip install -r requirements.txt
按需安裝
數(shù)據(jù)分析場景:
pip install pandas openpyxl xlwings
高性能場景:
pip install polars fastexcel
大數(shù)據(jù)場景:
pip install dask[complete] modin[all] vaex
注意事項
- xlrd版本問題:xlrd 2.0+ 不再支持xlsx格式,如需使用請安裝1.2.0版本
- xlwings依賴:需要安裝Microsoft Excel應(yīng)用程序
- Rust庫:calamine和fastexcel可能需要Rust編譯環(huán)境
- 內(nèi)存使用:大文件建議使用polars、dask等高性能庫
- 兼容性:某些庫在不同操作系統(tǒng)上的表現(xiàn)可能不同
選擇建議
新手/通用項目:使用 pandas
純Excel處理:使用 openpyxl
高性能需求:使用 polars 或 fastexcel
大數(shù)據(jù)處理:使用 dask 或 vaex
與Excel交互:使用 xlwings
多格式支持:使用 pyexcel
性能對比
| 庫名 | 小文件(<1MB) | 中文件(1-100MB) | 大文件(>100MB) | 內(nèi)存使用 |
|---|---|---|---|---|
| pandas | ???? | ??? | ?? | 中等 |
| openpyxl | ??? | ?? | ? | 較高 |
| polars | ????? | ????? | ????? | 低 |
| fastexcel | ????? | ????? | ???? | 低 |
| xlrd | ??? | ?? | ? | 中等 |
示例數(shù)據(jù)格式
所有示例都假設(shè)xlsx文件包含以下數(shù)據(jù):
| 姓名 | 年齡 | 城市 |
|---|---|---|
| 張三 | 25 | 北京 |
| 李四 | 30 | 上海 |
| 王五 | 35 | 廣州 |
讀取后的列表格式:
[
['姓名', '年齡', '城市'], # 列名(如果包含)
['張三', 25, '北京'],
['李四', 30, '上海'],
['王五', 35, '廣州']
]
完整代碼
"""
所有可以讀取xlsx文件的Python庫大全
每個庫都提供讀取xlsx文件每一行為列表的代碼示例
"""
# ============================================================================
# 1. pandas - 最流行的數(shù)據(jù)分析庫
# ============================================================================
def read_xlsx_with_pandas():
"""使用pandas讀取xlsx文件"""
import pandas as pd
# 讀取xlsx文件
df = pd.read_excel('example.xlsx')
# 方法1: 轉(zhuǎn)換為列表的列表
rows_as_lists = df.values.tolist()
# 方法2: 包含列名的方式
rows_with_headers = [df.columns.tolist()] + df.values.tolist()
# 方法3: 逐行讀取
rows = []
for index, row in df.iterrows():
rows.append(row.tolist())
return rows_as_lists
# ============================================================================
# 2. openpyxl - 專門處理Excel文件的庫
# ============================================================================
def read_xlsx_with_openpyxl():
"""使用openpyxl讀取xlsx文件"""
from openpyxl import load_workbook
# 加載工作簿
workbook = load_workbook('example.xlsx')
sheet = workbook.active # 或者 workbook['Sheet1']
# 讀取所有行為列表
rows = []
for row in sheet.iter_rows(values_only=True):
rows.append(list(row))
return rows
# ============================================================================
# 3. xlrd - 傳統(tǒng)的Excel讀取庫
# ============================================================================
def read_xlsx_with_xlrd():
"""使用xlrd讀取xlsx文件"""
import xlrd
# 打開工作簿
workbook = xlrd.open_workbook('example.xlsx')
sheet = workbook.sheet_by_index(0) # 或者 workbook.sheet_by_name('Sheet1')
# 讀取所有行
rows = []
for row_idx in range(sheet.nrows):
row = []
for col_idx in range(sheet.ncols):
row.append(sheet.cell_value(row_idx, col_idx))
rows.append(row)
return rows
# ============================================================================
# 4. xlwings - 與Excel應(yīng)用程序交互的庫
# ============================================================================
def read_xlsx_with_xlwings():
"""使用xlwings讀取xlsx文件"""
import xlwings as xw
# 打開Excel應(yīng)用程序和工作簿
app = xw.App(visible=False)
workbook = app.books.open('example.xlsx')
sheet = workbook.sheets[0]
# 獲取使用區(qū)域的數(shù)據(jù)
used_range = sheet.used_range
rows = used_range.value
# 如果只有一行數(shù)據(jù),確保返回列表的列表
if isinstance(rows[0], (int, float, str)):
rows = [rows]
# 關(guān)閉工作簿和應(yīng)用程序
workbook.close()
app.quit()
return rows
# ============================================================================
# 5. pyexcel - 統(tǒng)一的電子表格接口
# ============================================================================
def read_xlsx_with_pyexcel():
"""使用pyexcel讀取xlsx文件"""
import pyexcel
# 直接讀取為列表的列表
rows = pyexcel.get_records(file_name='example.xlsx')
# 轉(zhuǎn)換為純列表格式
if rows:
# 獲取列名
headers = list(rows[0].keys())
result = [headers]
# 添加數(shù)據(jù)行
for record in rows:
result.append([record[header] for header in headers])
return result
return []
# ============================================================================
# 6. xlsxwriter + openpyxl 組合 (xlsxwriter主要用于寫入,這里用openpyxl讀取)
# ============================================================================
def read_xlsx_with_xlsxwriter_openpyxl():
"""xlsxwriter主要用于寫入,讀取仍使用openpyxl"""
# xlsxwriter主要用于創(chuàng)建xlsx文件,讀取功能有限
# 通常與openpyxl結(jié)合使用
return read_xlsx_with_openpyxl()
# ============================================================================
# 7. python-excel - 另一個Excel處理庫
# ============================================================================
def read_xlsx_with_python_excel():
"""使用python-excel讀取xlsx文件"""
# 注意:python-excel通常指的是xlrd/xlwt/xlutils的組合
# 對于xlsx文件,推薦使用xlrd
return read_xlsx_with_xlrd()
# ============================================================================
# 8. tablib - 表格數(shù)據(jù)處理庫
# ============================================================================
def read_xlsx_with_tablib():
"""使用tablib讀取xlsx文件"""
import tablib
# 讀取xlsx文件
with open('example.xlsx', 'rb') as f:
dataset = tablib.Dataset().load(f, format='xlsx')
# 轉(zhuǎn)換為列表的列表
rows = []
if dataset.headers:
rows.append(dataset.headers)
for row in dataset:
rows.append(list(row))
return rows
# ============================================================================
# 9. xlutils - Excel工具集合
# ============================================================================
def read_xlsx_with_xlutils():
"""使用xlutils讀取xlsx文件"""
# xlutils主要用于xls文件,對xlsx支持有限
# 建議使用openpyxl或pandas
return read_xlsx_with_openpyxl()
# ============================================================================
# 10. ezodf - OpenDocument格式庫(也支持xlsx)
# ============================================================================
def read_xlsx_with_ezodf():
"""使用ezodf讀取xlsx文件"""
# ezodf主要用于ODF格式,對xlsx支持有限
# 建議使用專門的xlsx庫
return read_xlsx_with_openpyxl()
# ============================================================================
# 11. pyexcel-xlsx - pyexcel的xlsx插件
# ============================================================================
def read_xlsx_with_pyexcel_xlsx():
"""使用pyexcel-xlsx讀取xlsx文件"""
import pyexcel_xlsx
import pyexcel
# 讀取xlsx文件
sheet = pyexcel.get_sheet(file_name='example.xlsx')
# 轉(zhuǎn)換為列表的列表
rows = []
for row in sheet:
rows.append(list(row))
return rows
# ============================================================================
# 12. xlrd3 - xlrd的Python 3版本
# ============================================================================
def read_xlsx_with_xlrd3():
"""使用xlrd3讀取xlsx文件"""
# xlrd3是xlrd的分支,用法基本相同
return read_xlsx_with_xlrd()
# ============================================================================
# 13. calamine-python - Rust calamine的Python綁定
# ============================================================================
def read_xlsx_with_calamine():
"""使用calamine讀取xlsx文件"""
try:
from calamine import CalamineWorkbook
# 打開工作簿
workbook = CalamineWorkbook.from_path('example.xlsx')
# 讀取第一個工作表
sheet_names = workbook.sheet_names
if sheet_names:
sheet = workbook.get_sheet_by_name(sheet_names[0])
rows = []
for row in sheet.iter_rows():
rows.append(list(row))
return rows
except ImportError:
print("calamine庫未安裝,使用openpyxl替代")
return read_xlsx_with_openpyxl()
# ============================================================================
# 14. fastexcel - 快速Excel讀取庫
# ============================================================================
def read_xlsx_with_fastexcel():
"""使用fastexcel讀取xlsx文件"""
try:
import fastexcel
# 讀取Excel文件
df = fastexcel.read_excel('example.xlsx')
# 轉(zhuǎn)換為列表的列表
rows = df.values.tolist()
# 添加列名作為第一行
if hasattr(df, 'columns'):
rows.insert(0, df.columns.tolist())
return rows
except ImportError:
print("fastexcel庫未安裝,使用pandas替代")
return read_xlsx_with_pandas()
# ============================================================================
# 15. polars - 高性能數(shù)據(jù)處理庫
# ============================================================================
def read_xlsx_with_polars():
"""使用polars讀取xlsx文件"""
try:
import polars as pl
# 讀取xlsx文件
df = pl.read_excel('example.xlsx')
# 轉(zhuǎn)換為列表的列表
rows = df.to_numpy().tolist()
# 添加列名作為第一行
rows.insert(0, df.columns)
return rows
except ImportError:
print("polars庫未安裝,使用pandas替代")
return read_xlsx_with_pandas()
# ============================================================================
# 16. dask - 并行計算庫
# ============================================================================
def read_xlsx_with_dask():
"""使用dask讀取xlsx文件"""
try:
import dask.dataframe as dd
# dask不直接支持xlsx,需要先用pandas讀取
import pandas as pd
df = pd.read_excel('example.xlsx')
# 轉(zhuǎn)換為dask dataframe
ddf = dd.from_pandas(df, npartitions=1)
# 計算并轉(zhuǎn)換為列表
rows = ddf.compute().values.tolist()
return rows
except ImportError:
print("dask庫未安裝,使用pandas替代")
return read_xlsx_with_pandas()
# ============================================================================
# 17. modin - 加速pandas的庫
# ============================================================================
def read_xlsx_with_modin():
"""使用modin讀取xlsx文件"""
try:
import modin.pandas as pd
# 讀取xlsx文件
df = pd.read_excel('example.xlsx')
# 轉(zhuǎn)換為列表的列表
rows = df.values.tolist()
return rows
except ImportError:
print("modin庫未安裝,使用pandas替代")
return read_xlsx_with_pandas()
# ============================================================================
# 18. vaex - 大數(shù)據(jù)處理庫
# ============================================================================
def read_xlsx_with_vaex():
"""使用vaex讀取xlsx文件"""
try:
import vaex
import pandas as pd
# vaex不直接支持xlsx,需要先用pandas讀取
df_pandas = pd.read_excel('example.xlsx')
# 轉(zhuǎn)換為vaex dataframe
df_vaex = vaex.from_pandas(df_pandas)
# 轉(zhuǎn)換為列表的列表
rows = df_vaex.to_pandas_df().values.tolist()
return rows
except ImportError:
print("vaex庫未安裝,使用pandas替代")
return read_xlsx_with_pandas()
# ============================================================================
# 使用示例和測試函數(shù)
# ============================================================================
def create_sample_xlsx():
"""創(chuàng)建示例xlsx文件用于測試"""
import pandas as pd
# 創(chuàng)建示例數(shù)據(jù)
data = {
'姓名': ['張三', '李四', '王五'],
'年齡': [25, 30, 35],
'城市': ['北京', '上海', '廣州']
}
df = pd.DataFrame(data)
df.to_excel('example.xlsx', index=False)
print("已創(chuàng)建示例文件 example.xlsx")
def test_all_readers():
"""測試所有讀取方法"""
# 創(chuàng)建示例文件
create_sample_xlsx()
readers = [
("pandas", read_xlsx_with_pandas),
("openpyxl", read_xlsx_with_openpyxl),
("xlrd", read_xlsx_with_xlrd),
("pyexcel", read_xlsx_with_pyexcel),
("tablib", read_xlsx_with_tablib),
("pyexcel-xlsx", read_xlsx_with_pyexcel_xlsx),
("calamine", read_xlsx_with_calamine),
("fastexcel", read_xlsx_with_fastexcel),
("polars", read_xlsx_with_polars),
("dask", read_xlsx_with_dask),
("modin", read_xlsx_with_modin),
("vaex", read_xlsx_with_vaex),
]
for name, reader_func in readers:
try:
print(f"\n=== 使用 {name} 讀取 ===")
rows = reader_func()
for i, row in enumerate(rows):
print(f"第{i+1}行: {row}")
except Exception as e:
print(f"{name} 讀取失敗: {e}")
if __name__ == "__main__":
# 運(yùn)行測試
test_all_readers()
到此這篇關(guān)于Python讀取xlsx文件的所有Python庫大全(附代碼)的文章就介紹到這了,更多相關(guān)Python讀取xlsx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python編寫一個有趣的進(jìn)程勾選器(Process?Selector)
本文主要介紹了如何利用Python編寫一個有趣的進(jìn)程勾選器,可以在Checklistbox中列出系統(tǒng)中正在運(yùn)行的進(jìn)程的名稱和PID,并允許用戶選擇進(jìn)程并將其保存到文本文件中,需要的可以參考一下2023-05-05
使用PyQt5設(shè)計GUI實現(xiàn)程序圖形界面設(shè)計
當(dāng)我們學(xué)會如何在pycharm中配置pyqt5設(shè)計GU之后,那么本文來帶你熟悉PyQt5設(shè)計GUI流程并為程序設(shè)計圖形界面,設(shè)計一個屬于自己的GUI2021-08-08
pycharm使用conda創(chuàng)建的虛擬環(huán)境時找不到python.exe解決辦法
這篇文章主要給大家介紹了關(guān)于pycharm使用conda創(chuàng)建的虛擬環(huán)境時找不到python.exe的解決辦法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考價值,需要的朋友可以參考下2024-03-03
基于matplotlib中ion()和ioff()的使用詳解
這篇文章主要介紹了基于matplotlib中ion()和ioff()的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
使用PyInstaller如何打包一個包含多個文件的Python項目
這篇文章主要介紹了使用PyInstaller如何打包一個包含多個文件的Python項目,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
對python插入數(shù)據(jù)庫和生成插入sql的示例講解
今天小編就為大家分享一篇對python插入數(shù)據(jù)庫和生成插入sql的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
解決Cron定時任務(wù)中Pytest腳本無法發(fā)送郵件的問題
文章探討解決在 Cron 定時任務(wù)中運(yùn)行 Pytest 腳本時郵件發(fā)送失敗的問題,先優(yōu)化環(huán)境變量,再檢查 Pytest 郵件配置,接著配置文件確保 SMTP 服務(wù)正常,包括編輯相關(guān)文件、配置認(rèn)證信息等,還提及常見問題排查,如防火墻等,最終使郵件功能在定時任務(wù)中成功運(yùn)行2025-01-01
python 實現(xiàn)長數(shù)據(jù)完整打印方案
這篇文章主要介紹了python 實現(xiàn)長數(shù)據(jù)完整打印方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
django ORM之values和annotate使用詳解
這篇文章主要介紹了django ORM之values和annotate使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

