Messung der Breite und Höhe des Zellenwerts in Pixeln
Contents
[
Hide
]
Manchmal müssen Sie die Breite und Höhe des Zellenwerts berechnen, um den Zellenwert innerhalb der Zelle anzupassen. Aspose.Cells for Python via .NET bietet dafür Cell.get_width_of_value() und Cell.get_height_of_value() Methoden. Mit diesen Methoden können Sie die Breite und Höhe des Zellenwerts berechnen und dann die Breite der Spalte und Höhe der Zeile dieser Zelle entsprechend einstellen. Dadurch wird der Zellenwert innerhalb der Zelle angepasst oder eingepasst.
Alternativ können Sie auch Zeilen und Spalten Ihrer Zelle oder Zellenbereichs mit den Aspose.Cells for Python via .NET APIs automatisch anpassen.
Der folgende Code erläutert die Verwendung von Cell.get_width_of_value() und Cell.get_height_of_value() Methoden.
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
from aspose.cells import Workbook | |
# The path to the documents directory. | |
dataDir = "./" | |
# Create workbook object | |
workbook = Workbook() | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access cell B2 and add some value inside it | |
cell = worksheet.cells.get("B2") | |
cell.put_value("Welcome to Aspose!") | |
# Enlarge its font to size 16 | |
style = cell.get_style() | |
style.font.size = 16 | |
cell.set_style(style) | |
# Calculate the width and height of the cell value in unit of pixels | |
widthOfValue = cell.get_width_of_value() | |
heightOfValue = cell.get_height_of_value() | |
# Print both values | |
print("Width of Cell Value: " + str(widthOfValue)) | |
print("Height of Cell Value: " + str(heightOfValue)) | |
# Set the row height and column width to adjust/fit the cell value inside cell | |
worksheet.cells.set_column_width_pixel(1, widthOfValue) | |
worksheet.cells.set_row_height_pixel(1, heightOfValue) | |
# Save the output excel file | |
workbook.save(dataDir + "output_out.xlsx") |