Learn How to Convert Excel Range to List in Python using Aspose.Cells
Created
September 15, 2025 05:12
-
-
Save aspose-com-gists/7c0dcdd2ad2788b0f088f09027f99ebc to your computer and use it in GitHub Desktop.
How to Convert Excel Range to List in Python using Aspose.Cells
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from aspose.cells import Workbook | |
| # Step 1: Load the Excel workbook | |
| book = cells.Workbook("sample_data.xlsx") | |
| # Step 2: Access the first worksheet | |
| sheet1 = book.worksheets.get(0) | |
| # Step 3: Define the range (A1:C4 in this example) | |
| sheet_cells = sheet1.cells | |
| range_obj = sheet_cells.create_range("A1", "C4") | |
| # Step 4: Convert the range into a nested Python list | |
| 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) | |
| # Step 5: Print the Python list | |
| print("Python List Output:") | |
| print(range_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import Aspose.Cells library | |
| from aspose.cells import Workbook | |
| # Step 1: Load the Excel workbook from file | |
| book = Workbook("sample_data.xlsx") | |
| # Access the first worksheet in the workbook | |
| sheet = book.worksheets.get(0) | |
| # Step 2: Define the column index (0 = first column, i.e., Column A) | |
| col_index = 0 | |
| cells = sheet.cells | |
| # Create a range object for the selected column | |
| # Parameters: (start_row, start_column, total_rows, total_columns) | |
| # Here, start at row 0, select col_index, include all rows, and width = 1 column | |
| col_range = cells.create_range(0, col_index, sheet.cells.max_row + 1, 1) | |
| # Step 3 & 4: Convert the column into a Python list | |
| 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 the extracted column as a list | |
| print("Column to List:", col_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import Aspose.Cells library | |
| from aspose.cells import Workbook | |
| # Step 1: Load the Excel workbook from file | |
| book = Workbook("sample_data.xlsx") | |
| # Step 2: Access the first worksheet in the workbook | |
| sheet = book.worksheets.get(0) | |
| # Step 3: Define the row index (0 = first row, which contains headers) | |
| row_index = 0 | |
| cells = sheet.cells | |
| # Create a range object for the selected row | |
| row_range = cells.create_range(row_index, 0, 1, sheet.cells.max_column + 1) | |
| # Step 4: Convert the row into a Python list | |
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment