Bilder verwalten

Aspose.Cells für Python via .NET ermöglicht es Entwicklern, Bilder zur Laufzeit in Tabellen einzufügen. Außerdem kann die Positionierung dieser Bilder zur Laufzeit kontrolliert werden, was in den kommenden Abschnitten näher erläutert wird.

Dieser Artikel erklärt, wie man Bilder hinzufügt und wie man ein Bild einfügt, das den Inhalt bestimmter Zellen zeigt.

Bilder hinzufügen

Das Hinzufügen von Bildern zu einer Tabelle ist sehr einfach. Es dauert nur wenige Zeilen Code: Rufen Sie einfach die add-Methode der pictures-Sammlung (die im Worksheet-Objekt verkapselt ist) auf. Die add-Methode akzeptiert die folgenden Parameter:

  • Index der oberen linken Zeile, der Index der oberen linken Zeile.
  • Index der oberen linken Spalte, der Index der oberen linken Spalte.
  • Bilddateiname, der Name der Bilddatei inklusive des Pfads.
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")

Bilder positionieren

Es gibt zwei mögliche Wege, die Positionierung von Bildern mit Aspose.Cells für Python via .NET zu steuern:

  • Proportionale Positionierung: Definieren Sie eine Position proportional zur Zeilenhöhe und -breite.
  • Absolute Positionierung: Definieren Sie die genaue Position auf der Seite, an der das Bild eingefügt werden soll, z.B. 40 Pixel links und 20 Pixel unterhalb des Zellrandes.

Proportionale Positionierung

Entwickler können die Bilder proportional zur Zeilenhöhe und Spaltenbreite mithilfe der upper_delta_x- und upper_delta_y-Eigenschaften des Aspose.Cells.Drawing.Picture-Objekts positionieren. Ein Picture-Objekt kann aus der pictures-Sammlung erhalten werden, indem sein Bildindex übergeben wird. Dieses Beispiel platziert ein Bild in der Zelle 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")

Absolute Positionierung

Entwickler können auch die Bilder absolut positionieren, indem sie die Eigenschaften left und top des Picture-Objekts verwenden. Dieses Beispiel platziert ein Bild in der Zelle F6, 60 Pixel von links und 10 Pixel von oben der Zelle entfernt.

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")

Einfügen eines Bildes basierend auf Zellverweis

Aspose.Cells für Python via .NET ermöglicht es Ihnen, den Inhalt einer Tabellenzelle in eine Bildform zu rendern. Sie können das Bild mit der Zelle verlinken, die die Daten enthält, die Sie anzeigen möchten. Da die Zelle oder der Zellbereich mit dem Grafikobjekt verknüpft ist, werden Änderungen an den Daten in dieser Zelle oder diesem Zellbereich automatisch im Grafikobjekt angezeigt.

Fügen Sie ein Bild zum Arbeitsblatt hinzu, indem Sie die add_picture-Methode der ShapeCollection-Sammlung aufrufen (die im Worksheet-Objekt verkapselt ist). Geben Sie den Zellenbereich mit dem formula-Attribut des Picture-Objekts an.

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")

Erweiterte Themen