关于 Aspose.Cells Python via Net, 添加注释存在\n换行导致字体设置失效

Code
def add_comment(
self, cell_sheet: Worksheet,
cell_name: str, notes: Union[
str, List[str]
]
):
“”"
添加传统注释
:return: 注释对象
“”"

  if isinstance(notes, list):
      notes = '\n'.join(notes)

  comment: Comment = cell_sheet.comments[
      cell_sheet.comments.add(cell_name=cell_name)
  ]
  cmt_shape: CommentShape = comment.comment_shape
  comment.note = notes

  cmt_shape.fill.solid_fill.color = Color.white
  cmt_shape.width = 200
  cmt_shape.text_horizontal_overflow = TextOverflowType.CLIP
  cmt_shape.line_format.fore_color = Color.green

  comment.font.is_bold = False
  comment.font.name = '等线'
  comment.font.size = 11

理想情况
image.png (3.7 KB)

现实
image.png (2.6 KB)

@zengweiyu,

您需要根据注释/注解文本扩展注释形状的宽度和高度。这将确保换行符/换行符(使用 [“\n”] 字符)在正确的位置呈现/换行文本。请参阅以下示例代码以供参考。我测试过它,效果很好。

commentIndex = worksheet.comments.add("A1")
comment = worksheet.comments[commentIndex]
#  Set its vertical alignment setting
comment.comment_shape.text_vertical_alignment = TextAlignmentType.CENTER
#  Set the Comment note "This is second line Test" will be rendered in the second line"
comment.note = "This is my Comment Text. \nThis is second line Test."
shape = worksheet.comments.get(0, 0).comment_shape
shape.fill.solid_fill.color = Color.black
font = shape.font
font.color = Color.white
styleFlag = StyleFlag()
styleFlag.font_color = True
shape.text_body.format(0, len(shape.text), font, styleFlag)
#Set width and height accordingly
shape.width = 300
shape.height = 100

希望这能有所帮助。

我的代码里设置了字体和不加粗效果, 但是一遇到换行符, 就失效

@zengweiyu,

我测试了以下示例代码,效果很好。使用正确的字体设置,注释文本可以很好地呈现。

import aspose.cells
from aspose.cells import Workbook, WorksheetCollection, Worksheet, CellsHelper, License, SaveFormat,  StyleFlag, TextAlignmentType, BackgroundType
import io
from aspose.pydrawing import Color

# Create a new workbook
workbook = Workbook()

# Access the first worksheet
worksheet = workbook.worksheets[0]

# Add "Hello, World!" to cell A1
worksheet.cells.get("A1").put_value("Hello, World!")

#  Define a Style and get the A1 cell style
style = worksheet.cells.get("A1").get_style()
commentIndex = worksheet.comments.add("A1")
comment = worksheet.comments[commentIndex]
#  Set its vertical alignment setting
comment.comment_shape.text_vertical_alignment = TextAlignmentType.CENTER
#  Set the Comment note "This is second line Test" will be rendered in the second line"
comment.note = "This is my Comment Text. \nThis is second line Test. \nThis is third line"
shape = worksheet.comments.get(0, 0).comment_shape
shape.fill.solid_fill.color = Color.black
font = shape.font
font.color = Color.white
shape.font.is_bold = True
shape.font.name = "Calbri"
shape.font.size = 11

styleFlag = StyleFlag()
styleFlag.font_color = True
styleFlag.font_bold = True
styleFlag.font_name = True
styleFlag.font_size = True
shape.text_body.format(0, len(shape.text), font, styleFlag)
#Set width and height accordingly
shape.width = 300
shape.height = 100
# Create an io.BytesIO stream
stream = io.BytesIO()

# Save the workbook to the stream in Xlsx format
workbook.save(stream, SaveFormat.XLSX)

# Reset the stream position for further use
stream.seek(0)

# Optional: Save the stream content to a file to verify (for demonstration)
with open("e:\\test2\\HelloWorld.xlsx", "wb") as file:
    file.write(stream.getvalue())
# Create an io.BytesIO stream
stream = io.BytesIO()

# Save the workbook to the stream in Xlsx format
workbook.save(stream, SaveFormat.XLSX)

# Reset the stream position for further use
stream.seek(0)

# Optional: Save the stream content to a file to verify (for demonstration)
with open("e:\\test2\\HelloWorld.xlsx", "wb") as file:
    file.write(stream.getvalue())

请参考上面的代码片段,然后编写自己的代码。如果您仍然发现问题,请提供与上述相同的示例(可运行)代码来重现问题,我们会尽快检查。

1 Like

当文本溢出时, 如何让注释窗口自适应当前文本, 不至于产生遮盖呢

@zengweiyu,

您必须根据文本自行设置相应评论窗口的宽度和高度。该任务可能没有具体的自动扩展属性。

那么注释右上角的红色角标可以更改颜色吗

这个角标
image.png (3.2 KB)

@zengweiyu
在Excel文件中, 我们没有找到这个角标的颜色设置。
如果你能在Excel中找到相应的设置, 请告诉我们!