Converti Excel in dati Python

Come convertire il Workbook di Excel in un dizionario

Ecco uno snippet di codice di esempio per dimostrare come esportare i dati di Excel in un dizionario utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Attraversa tutti i fogli di lavoro e converti il workbook in un dizionario utilizzando la libreria Excel Python Aspose.Cells.
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)

Il risultato dell’output:

{'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]]}

Come convertire il Workbook di Excel in un elenco

Ecco un esempio di codice per dimostrare come esportare i dati di Excel in una lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Attraversa tutti i fogli di lavoro e converti il workbook in una lista utilizzando la libreria Excel di Aspose.Cells per Python.
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)

Il risultato dell’output:

[[['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]]] 

Come Convertire un Foglio di Lavoro in Lista

Ecco un esempio di codice per dimostrare come esportare i dati del foglio di lavoro in una lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Converti i dati del foglio di lavoro in una lista utilizzando la libreria Excel di Aspose.Cells per Python.
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)

Il risultato dell’output:

[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]

Come Convertire un ListObject di Excel in Lista

Ecco un esempio di codice per dimostrare come esportare i dati di ListObject in una lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Crea l’oggetto ListObject.
  4. Converti i dati di ListObject in una lista utilizzando la libreria Excel di Aspose.Cells per Python.
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)

Il risultato dell’output:

[['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]

Come Convertire una Riga di Excel in Lista

Ecco un esempio di codice per dimostrare come esportare i dati di Riga in una lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Ottieni l’oggetto riga per indice riga.
  4. Converti i dati della riga in lista usando la libreria Aspose.Cells per Excel in Python.
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)

Il risultato dell’output:

['Detroit', 'Central', 3074]

Come convertire una colonna di Excel in lista

Ecco un esempio di frammento di codice per dimostrare come esportare i dati della colonna in lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Ottenere l’oggetto colonna tramite l’indice della colonna.
  4. Converti i dati della colonna in lista usando la libreria Aspose.Cells per Excel in Python.
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)

Il risultato dell’output:

['Store', 3055, 3036, 3074]

Come convertire un intervallo di Excel in lista

Ecco un esempio di frammento di codice per dimostrare come esportare i dati dell’intervallo in lista utilizzando Aspose.Cells per Python via .NET:

  1. Caricare il file di esempio.
  2. Ottenere il primo foglio di lavoro.
  3. Crea l’intervallo.
  4. Converti i dati dell’intervallo in lista usando la libreria Aspose.Cells per Excel in Python.
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)

Il risultato dell’output:

[['Region', 'Store'], ['Central', 3055], ['East', 3036]]