ワークシート内の図形と一緒にテキストを回転する
可能な使用シナリオ
Microsoft Excelで任意の図形にテキストを追加できます。Microsoft Excel 2003という非常に古いバージョンで図形を追加すると、テキストは図形とともに回転しません。ただし、Microsoft Excelの新しいバージョン(2007、2010、2013、または2016など)を使用して図形を追加すると、テキストは図形とともに回転します。ShapeTextAlignment.rotate_text_with_shapeプロパティを使用して、テキストが図形と一緒に回転するかどうかを制御できます。これのデフォルト値はtrueです。これはテキストが図形と一緒に回転することを意味しますが、false に設定すると、テキストは図形と一緒に回転しません。
ワークシート内の図形と一緒にテキストを回転する
次のサンプルコードは、三角形の図形とそのテキストが図形とともに回転するサンプルエクセルファイルをロードします。サンプルエクセルファイルをMicrosoft Excelで開き、三角形の図形を回転させると、テキストも一緒に回転します。コード後に、ShapeTextAlignment.rotate_text_with_shapeプロパティをfalseに設定し、出力エクセルファイルとして保存します。今度は、出力エクセルファイルをMicrosoft Excelで開いて三角形の図形を回転させると、テキストは図形と一緒に回転しません。参考のために、コードの効果を示す以下のスクリーンショットをご覧ください。
サンプルコード
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") |