NumPyをExcelに変換する

NumPyの紹介

NumPy(Numerical Python)はPythonのオープンソースの数値演算拡張です。このツールは大きな行列を保存および処理するために使用でき、Pythonの階層リスト構造よりも効率的です(行列を表すためにも使用できます)。多数の次元配列および行列演算をサポートし、また、配列操作のための多数の数学関数ライブラリを提供します。

NumPyの主な機能:

  1. Ndarrayは、高速で柔軟性があり、データ構造を節約する多次元配列オブジェクトです。
  2. 行列乗算、転置、逆行列などの線形代数演算。
  3. 高速フーリエ変換、配列上で高速フーリエ変換を行います。
  4. 浮動小数点配列の高速操作。
  5. C言語コードをPythonに統合し、より速く実行します。

Aspose.Cells for Python via .NET APIを使用して、NumPy ndarrayをExcel、OpenOffice、Pdf、Jsonなど、さまざまな形式に変換できます。

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

Aspose.Cells for Python via .NETを使用して、NumPy配列からExcelファイルにデータをインポートする方法を示すコードスニペットの例です。

  1. サンプルの NumPy 配列データを作成します。
  2. NumPy 配列をトラバースして、Aspose.Cells for Python via .NET を使用してデータをインポートします。
import numpy as np
import pandas as pd
import aspose.cells
from aspose.cells import Workbook, Worksheet, Range, CellsHelper
def put_cell_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical):
table_row_index = row_index
table_column_index = column_index
row_count = len( table_data)
column_count = 0
for table_row in table_data:
for table_cell in table_row:
column_count =len( table_row)
put_cell_value(cells,table_cell,table_row_index,table_column_index)
if is_vertical :
table_row_index = table_row_index + 1
else:
table_column_index = table_column_index + 1
if is_vertical :
table_row_index = row_index
table_column_index = table_column_index + 1
else:
table_column_index = column_index
table_row_index = table_row_index + 1
if is_vertical :
end_row_index = row_index + row_count - 1
end_column_index = table_column_index - 1
else:
end_row_index = table_row_index - 1
end_column_index = column_index + column_count - 1
return (row_index,column_index,end_row_index,end_column_index)
excel_data = np.array([[['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]]] )
# Create a new Aspose.Cells Workbook
workbook = Workbook()
begin_row_index = 0
begin_column_index = 0
is_vertical = False
sheet_index = 1
for table_data in excel_data:
curr_sheet = workbook.worksheets.add("Sheet_data_" + str(sheet_index))
curr_cells = curr_sheet.cells
import_table_data_into_cells(curr_cells , table_data , begin_row_index,begin_column_index,is_vertical)
sheet_index += 1
workbook.save("out.xlsx")

出力結果:

NumPy ndarrayをワークシートに変換する方法

Aspose.Cells for Python via .NETを使用して、NumPy配列からExcelファイルにデータをインポートする方法を示すコードスニペットの例です。

  1. サンプルの NumPy 配列データを作成します。
  2. NumPy 配列をトラバースして、Aspose.Cells for Python via .NET を使用してデータをインポートします。
import numpy as np
import pandas as pd
import aspose.cells
from aspose.cells import Workbook, Worksheet, Range, CellsHelper
def put_cell_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical):
table_row_index = row_index
table_column_index = column_index
row_count = len( table_data)
column_count = 0
for table_row in table_data:
for table_cell in table_row:
column_count =len( table_row)
put_cell_value(cells,table_cell,table_row_index,table_column_index)
if is_vertical :
table_row_index = table_row_index + 1
else:
table_column_index = table_column_index + 1
if is_vertical :
table_row_index = row_index
table_column_index = table_column_index + 1
else:
table_column_index = column_index
table_row_index = table_row_index + 1
if is_vertical :
end_row_index = row_index + row_count - 1
end_column_index = table_column_index - 1
else:
end_row_index = table_row_index - 1
end_column_index = column_index + column_count - 1
return (row_index,column_index,end_row_index,end_column_index)
# Create a sample NumPy array
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]])
# Create a new Aspose.Cells Workbook
workbook = Workbook()
# Access the first (default) worksheet
worksheet = workbook.worksheets[0]
# Get the cells
cells = worksheet.cells
begin_row_index = 0
begin_column_index = 0
is_vertical = False
import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical)
workbook.save("out.xlsx")

出力結果:

NumPy ndarray を ListObject に変換する方法

Aspose.Cells for Python via .NET を使用して、NumPy 配列から ListObject にデータをインポートする方法を示すコードスニペットの例です:

  1. サンプルの NumPy 配列データを作成します。
  2. NumPy 配列をトラバースして、Aspose.Cells for Python via .NET を使用してデータをインポートします。
  3. サンプルデータを使用して ListObject オブジェクトを作成します。
import numpy as np
import pandas as pd
import aspose.cells
from aspose.cells import Workbook, Worksheet, Range, CellsHelper
def put_cell_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical):
table_row_index = row_index
table_column_index = column_index
row_count = len( table_data)
column_count = 0
for table_row in table_data:
for table_cell in table_row:
column_count =len( table_row)
put_cell_value(cells,table_cell,table_row_index,table_column_index)
if is_vertical :
table_row_index = table_row_index + 1
else:
table_column_index = table_column_index + 1
if is_vertical :
table_row_index = row_index
table_column_index = table_column_index + 1
else:
table_column_index = column_index
table_row_index = table_row_index + 1
if is_vertical :
end_row_index = row_index + row_count - 1
end_column_index = table_column_index - 1
else:
end_row_index = table_row_index - 1
end_column_index = column_index + column_count - 1
return (row_index,column_index,end_row_index,end_column_index)
# Create a sample NumPy array
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]])
# Create a new Aspose.Cells Workbook
workbook = Workbook()
# Access the first (default) worksheet
worksheet = workbook.worksheets[0]
# Get the cells
cells = worksheet.cells
begin_row_index = 0
begin_column_index = 0
is_vertical = False
# ndarray to listobject
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical)
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index)
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index)
index = worksheet.list_objects.add(start_cell, end_cell,True)
workbook.save("out.xlsx")

出力結果:

NumPy ndarray を Range に変換する方法

Aspose.Cells for Python via .NET を使用して、NumPy 配列から Range にデータをインポートする方法を示すコードスニペットの例です:

  1. サンプルの NumPy 配列データを作成します。
  2. NumPy 配列をトラバースして、Aspose.Cells for Python via .NET を使用してデータをインポートします。
  3. サンプルデータを使用して Range オブジェクトを作成します。
import numpy as np
import pandas as pd
import aspose.cells
from aspose.cells import Workbook, Worksheet, Range, CellsHelper
def put_cell_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical):
table_row_index = row_index
table_column_index = column_index
row_count = len( table_data)
column_count = 0
for table_row in table_data:
for table_cell in table_row:
column_count =len( table_row)
put_cell_value(cells,table_cell,table_row_index,table_column_index)
if is_vertical :
table_row_index = table_row_index + 1
else:
table_column_index = table_column_index + 1
if is_vertical :
table_row_index = row_index
table_column_index = table_column_index + 1
else:
table_column_index = column_index
table_row_index = table_row_index + 1
if is_vertical :
end_row_index = row_index + row_count - 1
end_column_index = table_column_index - 1
else:
end_row_index = table_row_index - 1
end_column_index = column_index + column_count - 1
return (row_index,column_index,end_row_index,end_column_index)
# Create a sample NumPy array
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]])
# Create a new Aspose.Cells Workbook
workbook = Workbook()
# Access the first (default) worksheet
worksheet = workbook.worksheets[0]
# Get the cells
cells = worksheet.cells
begin_row_index = 0
begin_column_index = 0
is_vertical = False
# ndarray to range
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical)
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index)
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index)
range_obj = cells.create_range(start_cell, end_cell)
print("row count: " + str(range_obj.row_count))
print("column count: " + str(range_obj.column_count))
workbook.save("out.xlsx")

出力結果:

row count: 4
column count: 3

NumPy ndarray を Name に変換する方法

Aspose.Cells for Python via .NET を使用して、NumPy 配列から Name にデータをインポートする方法を示すコードスニペットの例です:

  1. サンプルの NumPy 配列データを作成します。
  2. NumPy 配列をトラバースして、Aspose.Cells for Python via .NET を使用してデータをインポートします。
  3. サンプルデータを使用して Name オブジェクトを作成します。
import numpy as np
import pandas as pd
import aspose.cells
from aspose.cells import Workbook, Worksheet, Range, CellsHelper
def put_cell_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
def import_table_data_into_cells(cells,table_data,row_index,column_index,is_vertical):
table_row_index = row_index
table_column_index = column_index
row_count = len( table_data)
column_count = 0
for table_row in table_data:
for table_cell in table_row:
column_count =len( table_row)
put_cell_value(cells,table_cell,table_row_index,table_column_index)
if is_vertical :
table_row_index = table_row_index + 1
else:
table_column_index = table_column_index + 1
if is_vertical :
table_row_index = row_index
table_column_index = table_column_index + 1
else:
table_column_index = column_index
table_row_index = table_row_index + 1
if is_vertical :
end_row_index = row_index + row_count - 1
end_column_index = table_column_index - 1
else:
end_row_index = table_row_index - 1
end_column_index = column_index + column_count - 1
return (row_index,column_index,end_row_index,end_column_index)
# Create a sample NumPy array
data = np.array([['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]])
# Create a new Aspose.Cells Workbook
workbook = Workbook()
# Access the first (default) worksheet
worksheet = workbook.worksheets[0]
# Get the cells
cells = worksheet.cells
begin_row_index = 0
begin_column_index = 0
is_vertical = False
# ndarray to Name
(begin_row_index,begin_column_index,end_row_index,end_column_index) = import_table_data_into_cells(cells , data , begin_row_index,begin_column_index,is_vertical)
start_cell = CellsHelper.cell_index_to_name(begin_row_index, begin_column_index)
end_cell = CellsHelper.cell_index_to_name(end_row_index, end_column_index)
name_text = "Name_test"
name_refers_to = "=" + worksheet.name + "!$" + start_cell + ":$" + end_cell
name_index = worksheet.workbook.worksheets.names.add(name_text);
name = worksheet.workbook.worksheets.names[name_index]
name.refers_to = name_refers_to
workbook.save("out.xlsx")

出力結果: