NumPy'yi Excel'e dönüştür
Contents
[
Hide
]
Aspose.Cells for Python via .NET API’i kullanarak NumPy dizisini Excel, OpenOffice, Pdf, Json ve birçok farklı formata dönüştürebilirsiniz.
NumPy dizisini Excel’e dönüştürün
Aşağıda, Aspose.Cells for Python via .NET numaralı telefonu kullanarak bir NumPy dizisinden bir Excel dosyasına nasıl veri aktarılacağını gösteren örnek bir kod parçacığı verilmiştir:
- Örnek bir NumPy dizisi verisi oluşturun.
- NumPy dizisini çaprazlayın ve Aspose.Cells for Python via .NET’i kullanarak verileri içe aktarın.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from aspose.cells import Workbook, Worksheet | |
def put_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 | |
# Create a sample NumPy array | |
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) | |
# Create a new Aspose.Cells Workbook | |
workbook = Workbook() | |
# Access the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Get the cells | |
cells = worksheet.cells | |
# Import data from NumPy array to the worksheet | |
rowindex = -1 | |
colindex = 0 | |
for row in data: | |
rowindex += 1 | |
colindex = 0 | |
for item in row: | |
put_value(cells, item, rowindex, colindex) | |
print(item) | |
colindex += 1 | |
# Save the Excel file | |
workbook.save("out.xlsx") |