杰瑞科技汇

Python中如何用sheetbyname读取指定工作表?

Of course! The term sheetbyname is most famously associated with the xlrd library, an older but still used library for reading Excel files in Python.

Python中如何用sheetbyname读取指定工作表?-图1
(图片来源网络,侵删)

I'll cover:

  1. The Classic xlrd way (using sheet_by_name)
  2. The Modern & Recommended openpyxl way (using sheetnames and active)
  3. The Powerful pandas way (for data analysis)

The Classic xlrd Library

The xlrd library was the go-to for reading .xls and .xlsx files for a long time. Its method for accessing a sheet by name is very direct.

Installation

First, you need to install it:

pip install xlrd

How to Use sheet_by_name

The book.sheet_by_name('Sheet Name') method returns a worksheet object. You can then loop through its rows and cells.

Python中如何用sheetbyname读取指定工作表?-图2
(图片来源网络,侵删)

Important Note: xlrd version 2.0+ has limited support for .xlsx files (it can only read data, not formatting or formulas). For full .xlsx support, you might need an older version (e.g., xlrd==1.2.0) or switch to openpyxl.

import xlrd
# Path to your Excel file
file_path = 'your_workbook.xlsx'
try:
    # 1. Open the workbook
    workbook = xlrd.open_workbook(file_path)
    # 2. Get a sheet by its exact name
    # Be careful with the name! It must match exactly.
    sheet_name_to_find = 'Sheet1'
    worksheet = workbook.sheet_by_name(sheet_name_to_find)
    print(f"Successfully accessed sheet: '{worksheet.name}'")
    print(f"Number of rows: {worksheet.nrows}")
    print(f"Number of columns: {worksheet.ncols}")
    # 3. Read data from the sheet
    print("\n--- Reading cell values ---")
    # Loop through the first 5 rows
    for row_idx in range(min(5, worksheet.nrows)):
        # Get the row as a list of cell values
        row_values = worksheet.row_values(row_idx)
        print(f"Row {row_idx}: {row_values}")
    # You can also access a specific cell directly
    print("\n--- Accessing a specific cell ---")
    cell_value = worksheet.cell_value(0, 0)  # Row 0, Column 0
    print(f"Value at cell A1 (R0, C0): {cell_value}")
except xlrd.XLRDError:
    print(f"Error: The sheet '{sheet_name_to_find}' does not exist in the workbook.")
except FileNotFoundError:
    print(f"Error: The file '{file_path}' was not found.")

The Modern & Recommended openpyxl Library

openpyxl is the current standard for reading and writing .xlsx and .xlsm files. It's more actively maintained and provides better support for modern Excel features.

The process is slightly different: you first load the workbook, then get a list of all sheet names, and finally select the one you want.

Installation

pip install openpyxl

How to Access a Sheet by Name

from openpyxl import load_workbook
# Path to your Excel file
file_path = 'your_workbook.xlsx'
try:
    # 1. Load the workbook. 'data_only=True' reads the cell's value instead of the formula.
    workbook = load_workbook(filename=file_path, data_only=True)
    # 2. (Optional but recommended) See all available sheet names
    print("Available sheets:")
    for sheet_name in workbook.sheetnames:
        print(f"- {sheet_name}")
    # 3. Get a sheet by its name
    sheet_name_to_find = 'Sheet1'
    worksheet = workbook[sheet_name_to_find]
    print(f"\nSuccessfully accessed sheet: '{worksheet.title}'")
    print(f"Number of rows: {worksheet.max_row}")
    print(f"Number of columns: {worksheet.max_column}")
    # 4. Read data from the sheet
    print("\n--- Reading cell values ---")
    # Loop through rows and cells. openpyxl cells are 1-indexed.
    for row in worksheet.iter_rows(min_row=1, max_row=5, values_only=True):
        print(f"Row: {row}")
    # Access a specific cell
    print("\n--- Accessing a specific cell ---")
    # Cells can be accessed by letter (A1) or by row/column index (1, 1)
    cell_value = worksheet['A1'].value
    print(f"Value at cell A1: {cell_value}")
    # Don't forget to close the workbook
    workbook.close()
except KeyError:
    print(f"Error: The sheet '{sheet_name_to_find}' does not exist in the workbook.")
except FileNotFoundError:
    print(f"Error: The file '{file_path}' was not found.")

The Powerful pandas Library

If your goal is data analysis, loading an Excel sheet directly into a pandas DataFrame is often the most efficient approach. pandas uses openpyxl or xlrd in the background, so you get the best of both worlds.

Python中如何用sheetbyname读取指定工作表?-图3
(图片来源网络,侵删)

Installation

pip install pandas openpyxl

How to Use pandas.read_excel()

The sheet_name parameter in pandas.read_excel() does exactly what you're looking for.

import pandas as pd
# Path to your Excel file
file_path = 'your_workbook.xlsx'
try:
    # 1. Read a specific sheet directly into a DataFrame
    sheet_name_to_find = 'Sheet1'
    df = pd.read_excel(file_path, sheet_name=sheet_name_to_find, engine='openpyxl')
    print(f"Successfully loaded sheet '{sheet_name_to_find}' into a DataFrame.")
    print("\n--- DataFrame Info ---")
    print(df.info())
    print("\n--- First 5 rows of the DataFrame ---")
    print(df.head())
    # You can also read all sheets into a dictionary of DataFrames
    all_sheets = pd.read_excel(file_path, sheet_name=None, engine='openpyxl')
    print(f"\n--- All sheets loaded into a dictionary with keys: {list(all_sheets.keys())} ---")
    # Access a specific sheet from the dictionary
    sheet2_df = all_sheets.get('Sheet2')
    if sheet2_df is not None:
        print("\nFirst 5 rows of Sheet2:")
        print(sheet2_df.head())
except ValueError:
    print(f"Error: The sheet '{sheet_name_to_find}' does not exist in the workbook.")
except FileNotFoundError:
    print(f"Error: The file '{file_path}' was not found.")

Summary: Which One Should You Use?

Library Best For How to Access Sheet Pros Cons
xlrd Quick, simple reads of older .xls files or basic .xlsx data. workbook.sheet_by_name('name') Simple, direct API. Largely unmaintained. Poor .xlsx support in recent versions.
openpyxl Recommended. Reading/writing modern .xlsx files, manipulating styles, formulas, etc. workbook['sheet_name'] Actively maintained, full .xlsx support, powerful. Slightly more verbose for simple reads.
pandas Data Analysis. When you need to perform calculations, filtering, or statistical analysis on the data. pd.read_excel(..., sheet_name='name') Extremely powerful for data manipulation, industry standard. Overkill if you just need to read a single value.

For most new projects, especially if you're working with .xlsx files, start with openpyxl. If your primary goal is data analysis, use pandas.

分享:
扫描分享到社交APP
上一篇
下一篇