Excel を Python データに変換する

Excel ワークブックを Dictionary に変換する方法

Aspose.Cells for Python via .NET を使用して、Excel データを Dictionary にエクスポートする方法を示す例のコードスニペットです:

  1. サンプルファイルをロードします。
  2. すべてのワークシートをトラバースし、Aspose.Cells for Python Excel ライブラリを使用して Workbook を Dictionary に変換します。
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 via .NET を使用して、Excel データをリストにエクスポートする方法を示す例のコードスニペットです:

  1. サンプルファイルをロードします。
  2. 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 を使用してワークシートデータをリストにエクスポートする方法を示すコードスニペットの例です。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 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 via .NET を使用して ListObject データをリストにエクスポートする方法を示すコードスニペットの例です。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. ListObjectオブジェクトを作成します。
  4. 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 を使用して行データをリストにエクスポートする方法を示すコードスニペットの例です。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 行インデックスで行オブジェクトを取得します。
  4. 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 を使用して列データをリストにエクスポートする方法を示すコードスニペットの例です。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 列インデックスで列オブジェクトを取得します。
  4. 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を使用して範囲データをリストにエクスポートする方法を示すコードスニペットの例です。

  1. サンプルファイルをロードします。
  2. 最初のワークシートを取得します。
  3. 範囲を作成します。
  4. 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]]