Gestione delle immagini
Aspose.Cells per Python via .NET permette agli sviluppatori di aggiungere immagini ai fogli di calcolo in runtime. Inoltre, il posizionamento di queste immagini può essere controllato a runtime, come discusso nelle sezioni successive.
Questo articolo spiega come aggiungere immagini e come inserire un’immagine che mostra il contenuto di determinate celle.
Aggiunta di immagini
Aggiungere immagini a un foglio di calcolo è molto facile. Bastano poche righe di codice: Basta chiamare il metodo add della collezione pictures (incapsulata nell’oggetto Worksheet). Il metodo add accetta i seguenti parametri:
- Indice della riga in alto a sinistra, l’indice della riga in alto a sinistra.
- Indice della colonna in alto a sinistra, l’indice della colonna in alto a sinistra.
- Nome del file immagine, il nome del file immagine, completo di percorso.
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Posizionamento delle immagini
Ci sono due modi possibili per controllare il posizionamento delle immagini usando Aspose.Cells per Python via .NET:
- Posizionamento proporzionale: definire una posizione proporzionale all’altezza e alla larghezza della riga.
- Posizionamento assoluto: definire l’esatta posizione sulla pagina in cui l’immagine sarà inserita, ad esempio, 40 pixel a sinistra e 20 pixel sotto il bordo della cella.
Posizionamento proporzionale
Gli sviluppatori possono posizionare le immagini proporzionalmente all’altezza della riga e alla larghezza della colonna utilizzando le proprietà upper_delta_x e upper_delta_y dell’oggetto Aspose.Cells.Drawing.Picture. Un oggetto Picture può essere ottenuto dalla raccolta pictures passando l’indice dell’immagine. Questo esempio posiziona un’immagine nella cella F6.
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Positioning the picture proportional to row height and colum width | |
picture.upper_delta_x = 200 | |
picture.upper_delta_y = 200 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Posizionamento Assoluto
Gli sviluppatori possono anche posizionare le immagini in modo assoluto utilizzando le proprietà left e top dell’oggetto Picture. Questo esempio posiziona un’immagine nella cella F6, 60 pixel a sinistra e 10 pixel dall’alto della cella.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a picture at the location of a cell whose row and column indices | |
# Are 5 in the worksheet. It is "F6" cell | |
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg") | |
# Accessing the newly added picture | |
picture = worksheet.pictures[pictureIndex] | |
# Absolute positioning of the picture in unit of pixels | |
picture.left = 60 | |
picture.top = 10 | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Inserimento di un’immagine in base al riferimento della cella
Aspose.Cells per Python via .NET ti permette di visualizzare il contenuto di una cella del foglio di lavoro come forma immagine. Puoi collegare l’immagine alla cella che contiene i dati che desideri visualizzare. Dato che la cella, o l’intervallo di celle, è collegata all’oggetto grafico, le modifiche apportate ai dati in quella cella o intervallo di celle appaiono automaticamente nell’oggetto grafico.
Aggiungi un’immagine al foglio di lavoro chiamando il metodo add_picture della raccolta ShapeCollection (incapsulata nell’oggetto Worksheet). Specifica il range di celle utilizzando l’attributo formula dell’oggetto Picture.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook | |
workbook = Workbook() | |
# Get the first worksheet's cells collection | |
cells = workbook.worksheets[0].cells | |
# Add string values to the cells | |
cells.get("A1").put_value("A1") | |
cells.get("C10").put_value("C10") | |
# Add a blank picture to the D1 cell | |
pic = workbook.worksheets[0].shapes.add_picture(0, 3, 10, 6, None) | |
# Specify the formula that refers to the source range of cells | |
pic.formula = "A1:C10" | |
# Update the shapes selected value in the worksheet | |
workbook.worksheets[0].shapes.update_selected_value() | |
# Save the Excel file. | |
workbook.save(dataDir + "output.out.xls") |