Konvertieren Sie NumPy in Excel

Einführung in NumPy

NumPy (Numerisches Python) ist eine Open-Source-numerische Rechenerweiterung für Python. Dieses Tool kann verwendet werden, um große Matrizen zu speichern und zu verarbeiten, was effizienter ist als die verschachtelte Listenstruktur von Python (die ebenfalls zur Darstellung von Matrizen verwendet werden kann). Es unterstützt eine große Anzahl von Dimensionalarrays und Matrixoperationen und bietet auch eine große Anzahl von mathematischen Funktionsbibliotheken für Arrayoperationen.

Die Hauptfunktionen von NumPy:

  1. Ndarray, ein mehrdimensionales Array-Objekt, ist eine schnelle, flexible und platzsparende Datenstruktur.
  2. Lineare Algebraoperationen, einschließlich Matrixmultiplikation, Transposition, Inversion usw.
  3. Fourier-Transformation, Durchführung einer schnellen Fourier-Transformation an einem Array.
  4. Schnelle Verarbeitung von Gleitkommazahlenarrays.
  5. Integration von C-Sprachencode in Python, um die Ausführung zu beschleunigen.

Mit der Aspose.Cells für Python via .NET API können Sie NumPy ndarray in Excel, OpenOffice, Pdf, Json und viele verschiedene Formate konvertieren.

Wie man NumPy ndarray in Excel-Arbeitsmappe konvertiert

Hier ist ein Beispielcode-Schnipsel, um zu demonstrieren, wie Daten aus einem NumPy-Array mithilfe von Aspose.Cells für Python via .NET in eine Excel-Datei importiert werden.

  1. Erstellen Sie Beispieldaten eines NumPy-Arrays.
  2. Durchlaufen Sie das NumPy-Array und importieren Sie die Daten mithilfe von Aspose.Cells für 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")

Das Ausgabenergebnis:

Wie man NumPy ndarray in ein Arbeitsblatt konvertiert

Hier ist ein Beispielcode-Schnipsel, um zu demonstrieren, wie Daten aus einem NumPy-Array mithilfe von Aspose.Cells für Python via .NET in eine Excel-Datei importiert werden.

  1. Erstellen Sie Beispieldaten eines NumPy-Arrays.
  2. Durchlaufen Sie das NumPy-Array und importieren Sie die Daten mithilfe von Aspose.Cells für 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")

Das Ausgabenergebnis:

So konvertieren Sie ein NumPy-Array in ein ListObject

Hier ist ein Beispielcode-Schnipsel, um zu demonstrieren, wie Daten aus einem NumPy-Array mithilfe von Aspose.Cells für Python via .NET in ein ListObject importiert werden.

  1. Erstellen Sie Beispieldaten eines NumPy-Arrays.
  2. Durchlaufen Sie das NumPy-Array und importieren Sie die Daten mithilfe von Aspose.Cells für Python via .NET.
  3. Erstellen Sie ein ListObject-Objekt mit Beispieldaten.
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")

Das Ausgabenergebnis:

So konvertieren Sie ein NumPy-Array in einen Bereich

Hier ist ein Beispielcode-Schnipsel, um zu demonstrieren, wie Daten aus einem NumPy-Array mithilfe von Aspose.Cells für Python via .NET in einen Bereich importiert werden.

  1. Erstellen Sie Beispieldaten eines NumPy-Arrays.
  2. Durchlaufen Sie das NumPy-Array und importieren Sie die Daten mithilfe von Aspose.Cells für Python via .NET.
  3. Erstellen Sie einen Bereich mit Beispieldaten.
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")

Das Ausgabenergebnis:

row count: 4
column count: 3

So konvertieren Sie ein NumPy-Array in einen Namen

Hier ist ein Beispielcode-Schnipsel, um zu demonstrieren, wie Daten aus einem NumPy-Array mithilfe von Aspose.Cells für Python via .NET in einen Namen importiert werden.

  1. Erstellen Sie Beispieldaten eines NumPy-Arrays.
  2. Durchlaufen Sie das NumPy-Array und importieren Sie die Daten mithilfe von Aspose.Cells für Python via .NET.
  3. Erstellen Sie ein Name-Objekt unter Verwendung von Beispieldaten.
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")

Das Ausgabenergebnis: