亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python讀取xlsx文件的所有Python庫大全(附代碼)

 更新時間:2025年11月03日 09:29:37   作者:小莊-Python辦公  
這篇文章收集了所有可以讀取xlsx文件的Python庫,并提供了每個庫讀取xlsx文件每一行為列表的代碼示例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下

文件說明

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,速度快大文件快速讀取
calamineRust實現(xiàn),高性能性能要求極高的場景
dask并行計算超大文件處理
modin加速pandaspandas性能優(yōu)化
vaex大數(shù)據(jù)可視化億級數(shù)據(jù)處理

傳統(tǒng)庫

庫名特點適用場景
xlrd傳統(tǒng)Excel讀取庫老項目維護(hù)(注意版本兼容性)
xlutilsExcel工具集配合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

高性能需求:使用 polarsfastexcel

大數(shù)據(jù)處理:使用 daskvaex

與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)文章

最新評論