Convertir Excel en données Python

Comment convertir un classeur Excel en dictionnaire

Voici un extrait de code d’exemple pour démontrer comment exporter des données Excel en dictionnaire en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Parcourir toutes les feuilles de calcul et convertir le classeur en dictionnaire en utilisant la bibliothèque Excel Python d’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)

Le résultat en sortie :

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

Comment convertir un classeur Excel en liste

Voici un extrait de code d’exemple pour démontrer comment exporter des données Excel en liste en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Parcourir toutes les feuilles de calcul et convertir le classeur en liste en utilisant la bibliothèque Excel Python d’Aspose.Cells.
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)

Le résultat en sortie :

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

Comment convertir une feuille de calcul en liste

Voici un extrait de code d’exemple pour démontrer comment exporter des données de feuille de calcul en liste en utilisant Aspose.Cells pour Python via .NET :

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Convertir les données de la feuille de calcul en liste en utilisant la bibliothèque Excel Aspose.Cells pour 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)

Le résultat en sortie :

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

Comment convertir un objet ListObject d’Excel en liste

Voici un exemple de code pour démontrer comment exporter les données ListObject en liste en utilisant Aspose.Cells pour la bibliothèque Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Créer un objet ListObject.
  4. Convertir les données de l’objet ListObject en liste en utilisant la bibliothèque Excel Aspose.Cells pour 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)

Le résultat en sortie :

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

Comment convertir une ligne d’Excel en liste

Voici un exemple de code pour démontrer comment exporter les données de ligne en liste en utilisant Aspose.Cells pour la bibliothèque Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Obtenir un objet Row par indice de ligne.
  4. Convertir les données de la ligne en liste en utilisant la bibliothèque Excel Aspose.Cells pour 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)

Le résultat en sortie :

['Detroit', 'Central', 3074]

Comment convertir une colonne d’Excel en liste

Voici un exemple de code pour démontrer comment exporter les données de colonne en liste en utilisant Aspose.Cells pour la bibliothèque Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Obtenir un objet Column par indice de colonne.
  4. Convertissez les données de colonne en liste en utilisant la bibliothèque Excel Aspose.Cells for 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)

Le résultat en sortie :

['Store', 3055, 3036, 3074]

Comment convertir une plage d’Excel en liste

Voici un extrait de code d’exemple pour démontrer comment exporter des données de plage en liste en utilisant Aspose.Cells pour Python via .NET:

  1. Charger le fichier d’exemple.
  2. Obtenir la première feuille de calcul.
  3. Créer la plage.
  4. Convertissez les données de plage en liste en utilisant la bibliothèque Excel Aspose.Cells pour 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)

Le résultat en sortie :

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