将Excel转换为Python数据
如何将Excel工作簿转换为字典
以下是一个示例代码片段,演示了如何使用Aspose.Cells for Python via .NET将Excel数据导出为字典:
- 加载样本文件。 1.遍历所有工作表,使用Aspose.Cells for Python Excel库将工作簿转换为字典。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# excel to Dictionary | |
sheet_count = book.worksheets.capacity - 1 | |
excel_dict = {} | |
for sheet_index in range(0, sheet_count): | |
worksheet_list =[] | |
sheet = book.worksheets.get(sheet_index) | |
cells = sheet.cells | |
rows = cells.rows | |
max_column_index = cells.max_column + 1 | |
row_count = rows.count | |
index = -1 | |
for row_index in range(0, row_count): | |
row = rows.get_row_by_index(row_index) | |
if row_index != row.index: | |
for blank_row_index in range(index+1, row.index): | |
blank_row =[] | |
for blank_column_index in range(0,max_column_index): | |
blank_row.append("") | |
worksheet_list.append(blank_row) | |
data_row =[] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row.index, column_index) | |
if curr_cell: | |
data_row.append(curr_cell.value) | |
else: | |
data_row.append("") | |
worksheet_list.append(data_row) | |
index = row.index | |
excel_dict[sheet.name] = worksheet_list | |
print(excel_dict) |
输出结果:
{'Sheet1': [['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]], 'Sheet2': [['City2', 'Region2', 'Store3'], ['Seattle', 'West', 3000], ['philadelph', 'East', 3082], ['Detroit', 'Central', 3074]], 'Sheet3': [['City3', 'Region3', 'Store3'], ['Seattle', 'West', 3166], ['New York', 'East', 3090], ['Chicago', 'Central', 3055]]}
如何将Excel工作簿转换为列表
以下是一个示例代码片段,演示了如何使用Aspose.Cells for Python将Excel数据导出到列表中via .NET:
- 加载样本文件。
- 遍历所有工作表,并使用Aspose.Cells for Python Excel库将工作簿转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# workbook to list | |
sheet_count = book.worksheets.capacity - 1 | |
workbook_list = [] | |
for sheet_index in range(0, sheet_count): | |
worksheet_list =[] | |
sheet = book.worksheets.get(sheet_index) | |
cells = sheet.cells | |
rows = cells.rows | |
max_column_index = cells.max_column + 1 | |
row_count = rows.count | |
index = -1 | |
for row_index in range(0, row_count): | |
row = rows.get_row_by_index(row_index) | |
if row_index != row.index: | |
for blank_row_index in range(index+1, row.index): | |
blank_row =[] | |
for blank_column_index in range(0,max_column_index): | |
blank_row.append("") | |
worksheet_list.append(blank_row) | |
data_row =[] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row.index, column_index) | |
if curr_cell: | |
data_row.append(curr_cell.value) | |
else: | |
data_row.append("") | |
worksheet_list.append(data_row) | |
index = row.index | |
workbook_list.append(worksheet_list) | |
print(workbook_list) |
输出结果:
[[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]],
[['City2', 'Region2', 'Store3'], ['Seattle', 'West', 3000], ['philadelph', 'East', 3082], ['Detroit', 'Central', 3074]], [['City3', 'Region3', 'Store3'], ['Seattle', 'West', 3166], ['New York', 'East', 3090], ['Chicago', 'Central', 3055]]]
如何将工作表转换为列表
以下是一个示例代码片段,演示了如何使用Aspose.Cells for Python将工作表数据导出到列表中via .NET:
- 加载样本文件。
- 获取第一个工作表。
- 使用Aspose.Cells for Python Excel库将工作表数据转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
rows = cells.rows | |
max_column_index = sheet1.cells.max_column + 1 | |
# worksheet to list | |
worksheet_list =[] | |
row_count = rows.count | |
index = -1 | |
for row_index in range(0, row_count): | |
row = rows.get_row_by_index(row_index) | |
if row_index != row.index: | |
for blank_row_index in range(index+1, row.index): | |
blank_row =[] | |
for blank_column_index in range(0,max_column_index): | |
blank_row.append("") | |
worksheet_list.append(blank_row) | |
data_row =[] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row.index, column_index) | |
if curr_cell: | |
data_row.append(curr_cell.value) | |
else: | |
data_row.append("") | |
worksheet_list.append(data_row) | |
index = row.index | |
print(worksheet_list) |
输出结果:
[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]
如何将Excel的ListObject转换为列表
以下是一个示例代码片段,演示了如何使用Aspose.Cells for Python将ListObject数据导出到列表中via .NET:
- 加载样本文件。
- 获取第一个工作表。
- 创建ListObject对象。
- 使用Aspose.Cells for Python Excel库将ListObject数据转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
# listobject to list | |
table_index = sheet1.list_objects.add("A1", "C4", True) | |
table = sheet1.list_objects[table_index] | |
table_list =[] | |
for row_index in range(table.start_row , table.end_row + 1): | |
row =[] | |
for column_index in range(table.start_column, table.end_column + 1): | |
curr_cell = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row.append(curr_cell.value) | |
else: | |
row.append("") | |
table_list.append(row) | |
print(table_list) |
输出结果:
[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]
如何将Excel的行转换为列表
以下是一个示例代码片段,演示了如何使用Aspose.Cells for Python将行数据导出到列表中via .NET:
- 加载样本文件。
- 获取第一个工作表。
- 通过行索引获取行对象。
- 使用Aspose.Cells for Python Excel库将行数据转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
# row to list | |
max_column_index = sheet1.cells.max_column + 1 | |
row_index = cells.max_data_row | |
row_list = [] | |
for column_index in range(0,max_column_index): | |
curr_cell = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row_list.append(curr_cell.value) | |
else: | |
row_list.append("") | |
print(row_list) |
输出结果:
['Detroit', 'Central', 3074]
如何将Excel列转换为列表
以下是一个代码片段示例,演示如何使用Aspose.Cells for Python via .NET将列数据导出为列表:
- 加载样本文件。
- 获取第一个工作表。
- 通过列索引获取列对象。
- 使用Aspose.Cells for Python Excel库将列数据转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
cells = sheet1.cells | |
# column to list | |
max_row_index = sheet1.cells.max_row + 1 | |
column_index = cells.max_data_column | |
column_list = [] | |
for row_index in range(0,max_row_index): | |
curr_cell = sheet1.cells.check_cell(row_index, column_index) | |
if curr_cell: | |
column_list.append(curr_cell.value) | |
else: | |
column_list.append("") | |
print(column_list) |
输出结果:
['Store', 3055, 3036, 3074]
如何将Excel范围转换为列表
以下是一个代码片段示例,演示如何使用Aspose.Cells for Python via .NET将范围数据导出为列表:
- 加载样本文件。
- 获取第一个工作表。
- 创建范围。
- 使用Aspose.Cells for Python Excel库将范围数据转换为列表。
import aspose.cells | |
from aspose.cells import Workbook, Worksheet, Range | |
# Open the Excel workbook | |
book = Workbook("sample_data.xlsx") | |
# Get the first worksheet | |
sheet1 = book.worksheets.get(0) | |
# range to list | |
cells = sheet1.cells | |
range_obj = cells.create_range("B1", "C3") | |
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 = cells.check_cell(row_index, column_index) | |
if curr_cell: | |
row.append(curr_cell.value) | |
else: | |
row.append("") | |
range_list.append(row) | |
print(range_list) |
输出结果:
[['Region', 'Store'], ['Central', 3055], ['East', 3036]]