在 Python 中使用 Excel 數據通常需要將特定的行和列提取到列表格式中。將 Excel 範圍轉換為 Python 列表對於以下任務非常有用:
- 使用 Pandas 和 NumPy 進行數據分析
- 報告和 ETL 過程的自動化
- 與機器學習模型或API的整合
在這篇文章中,我們將逐步學習如何將定義的 Excel 範圍轉換為 Python 中的列表。
Python Excel 轉 List 轉換器庫
開發者可以使用 Aspose.Cells for Python via .NET,這是一個強大的 Excel 到 List 轉換庫,而不是手動解析 Excel 文件。它不僅使提取範圍、行和列到 Python 列表變得更容易,還支持如公式、格式、圖表和樞紐分析表等高級功能,即使在複雜的電子表格中也能確保準確性。
在編碼之前,確保您的設置已準備好:
- 安裝 Python 3.7 以上版本。
- 從 releases 下載 Aspose.Cells 或使用 pip 安裝:
pip install aspose-cells-python
- 準備一個範例 Excel 文件 (
sampledata.xlsx),內容如下:

樣本 Excel 數據文件。
將 Excel 範圍轉換為 Python 列表:分步指南
我們來看看如何使用 Aspose.Cells for Python 將一範圍的 Excel 數據轉換為 Python 列表的過程。
按照以下步驟將 Excel 範圍轉換為 Python 中的列表:
- 首先,使用
Workbook類別加載現有的 Excel 文件。 - 第二,獲取第一個工作表。
- 接下來,建立一個範圍,例如 A1 到 C4。
- 之後,將 Range 轉換為 Python List。
- 最後,列印清單。
以下的 Python 腳本加載 Excel 文件,定義一個範圍,並將其轉換為 Python 列表。
from aspose.cells import Workbook
# 步驟 1:加載 Excel 工作簿
book = cells.Workbook("sample_data.xlsx")
# 步驟 2:訪問第一個工作表
sheet1 = book.worksheets.get(0)
# 步驟 3:定義範圍 (A1:C4 在此範例中)
sheet_cells = sheet1.cells
range_obj = sheet_cells.create_range("A1", "C4")
# 步驟 4:將範圍轉換為嵌套的 Python 列表
range_list = []
for row_index in range(range_obj.first_row, range_obj.first_row + range_obj.row_count):
row = []
for column_index in range(range_obj.first_column, range_obj.first_column + range_obj.column_count):
curr_cell = sheet_cells.check_cell(row_index, column_index)
row.append(curr_cell.value if curr_cell else "")
range_list.append(row)
# 步驟 5:列印 Python 列表
print("Python List Output:")
print(range_list)
Output
Python List Output:
[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]
這個完整的腳本顯示了如何從 Excel 中提取數據並將其轉換為 Python 列表。之後,根據您的需求,它可以輕鬆地轉換為 Pandas 或 JSON。
將 Python 列表轉換為 Pandas DataFrame
使用 Pandas,您可以直接將列表轉換為 DataFrame:
import pandas as pd
# Convert to a Pandas DataFrame
df = pd.DataFrame(range_list[1:], columns=range_list[0])
print(df)
Pandas DataFrame 輸出:
City Region Store
0 Chicago Central 3055
1 New York East 3036
2 Detroit Central 3074
保存 Python 列表為 JSON
您也可以將數據導出為 JSON:
import json
# Convert to JSON
json_output = json.dumps(range_list)
print(json_output)
JSON 輸出:
[["City", "Region", "Store"], ["Chicago", "Central", 3055], ["New York", "East", 3036], ["Detroit", "Central", 3074]]
將 Excel 列轉換為 Python 中的列表
有時您可能只想從 Excel 中提取單行並將其存儲為列表。以下是使用 Aspose.Cells 的方法:
- 載入 Excel 工作簿。
- 訪問目標工作表。
- 根據索引選擇行。
- 將行值收集到 Python 列表中。
# 導入 Aspose.Cells 函式庫
from aspose.cells import Workbook
# 步驟 1:從文件中加載 Excel 工作簿
book = Workbook("sample_data.xlsx")
# 步驟 2:訪問工作簿中的第一個工作表
sheet = book.worksheets.get(0)
# 步驟 3:定義行索引(0 = 第一行,該行包含標題)
row_index = 0
cells = sheet.cells
# 為所選行創建範圍對象
row_range = cells.create_range(row_index, 0, 1, sheet.cells.max_column + 1)
# 步驟 4:將該行轉換為 Python 列表
row_list = []
for column_index in range(row_range.first_column, row_range.first_column + row_range.column_count):
curr_cell = cells.check_cell(row_index, column_index) # Get each cell in the row
row_list.append(curr_cell.value if curr_cell else "") # Append value or empty string if cell is blank
# Print the extracted row as a list
print("Row to List:", row_list)
Output:
Row to List: ['City', 'Region', 'Store']
將 Excel 列轉換為 Python 中的列表
您也可以將單一列提取為列表。例如,讓我們將 Region 列轉換為列表:
- 載入工作簿和工作表。
- 選擇索引的列。
- 遍歷該列中的每一行。
- 將列值收集到一個列表中。
# 匯入 Aspose.Cells 函式庫
from aspose.cells import Workbook
# 步驟 1:從檔案中加載 Excel 工作簿
book = Workbook("sample_data.xlsx")
# 訪問工作簿中的第一個工作表
sheet = book.worksheets.get(0)
# 第 2 步:定義列索引 (0 = 第一列,即 A 列)
col_index = 0
cells = sheet.cells
# 為所選列創建範圍對象
# 參數: (起始行, 起始列, 總行數, 總列數)
# 在這裡,從第 0 行開始,選擇 colindex,包括所有行,並將寬度設為 1 列
col_range = cells.create_range(0, col_index, sheet.cells.max_row + 1, 1)
# 步驟 3 和 4:將該列轉換為 Python 列表
col_list = []
for row_index in range(col_range.first_row, col_range.first_row + col_range.row_count):
curr_cell = cells.check_cell(row_index, col_index) # Get each cell in the column
if curr_cell: # Only add if the cell exists (ignore empty rows)
col_list.append(curr_cell.value)
# 將提取的列打印為列表
print("Column to List:", col_list)
Output:
Column to List: ['City', 'Chicago', 'New York', 'Detroit']
獲得免費許可證
評估 Aspose.Cells for Python via .NET,無任何限制。從 license page 請求免費臨時許可證。將其應用於您的程式碼中以移除評估限制。測試每一個功能,包括 DF 到 Excel、圖表、公式和大型檔案。
Excel to List: 免費資源
利用以下資源來加深您的知識,增強您的理解,並獲得實用的見解,幫助您更有效地應用所學。
結論
我們展示了如何通過 Aspose.Cells for Python via .NET 將 Excel 數據轉換為 Python 列表,通過提取範圍、行和列。一旦轉換為列表格式,這些數據可以用於 Pandas、JSON 或其他處理任務。雖然像 openpyxl 或 pandas.readexcel 這樣的庫可以提取範圍,但 Aspose.Cells 提供了對公式、格式、圖表和合併單元格的更多控制,使其成為複雜 Excel 操作的更好選擇。
如果您需要幫助或有任何問題,請隨時在我們的 Aspose.Cells Free Support Forum 聯繫我們。我們的團隊將樂意協助您。
