在工作表内旋转文本与形状

可能的使用场景

您可以在Microsoft Excel中的任何形状中添加文本。如果您使用非常旧的Microsoft Excel 2003添加形状,则文本不会随形状旋转。但是,如果您使用较新的版本的Microsoft Excel,例如2007、2010、2013或2016等添加形状,则文本将随形状旋转。您可以使用ShapeTextAlignment.rotate_text_with_shape属性控制文本是否应随形状旋转。其默认值为true,这意味着文本将随形状旋转,但如果将其设置为false,则文本将不会随形状旋转。

在工作表内旋转文本与形状

以下示例代码加载包含三角形形状及其文本随形状旋转的示例Excel文件(64716896.xlsx)。如果在Microsoft Excel中打开示例Excel文件并旋转三角形形状,则文本也将随之旋转。然后将ShapeTextAlignment.rotate_text_with_shape属性设置为false并保存为输出Excel文件。现在,在Microsoft Excel中打开输出Excel文件并旋转三角形形状,文本将不会随之旋转。请参阅以下屏幕截图,以了解代码对示例Excel文件的影响。

todo:image_alt_text

示例代码

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Load sample Excel file.
wb = Workbook(sourceDir + "sampleRotateTextWithShapeInsideWorksheet.xlsx")
# Access first worksheet.
ws = wb.worksheets[0]
# Access cell B4 and add message inside it.
b4 = ws.cells.get("B4")
b4.put_value("Text is not rotating with shape because RotateTextWithShape is false.")
# Access first shape.
sh = ws.shapes[0]
# Access shape text alignment.
shapeTextAlignment = sh.text_body.text_alignment
# Do not rotate text with shape by setting RotateTextWithShape as false.
shapeTextAlignment.rotate_text_with_shape = False
# Save the output Excel file.
wb.save(outputDir + "outputRotateTextWithShapeInsideWorksheet.xlsx")