自动调整大小评论框

Auto size comment box

我正在使用 win32com 模块编写 excel 文件。我正在尝试的任务之一是向单元格添加评论并自动调整评论框的大小。如果未调整大小,则将鼠标悬停在单元格上时,部分注释将不可见。这是我目前所拥有的:

from win32com.client import Dispatch
import os

dirpath = os.path.dirname(os.path.realpath(__file__))
outputfile = 'ExcelTest.xlsx'
excel = Dispatch("Excel.Application")

wb = excel.Workbooks.Add()
ws = wb.Worksheets('Sheet1')
ws.Name = 'Test'

ws.Cells(1, 1).Value = 'Hi'
rng = ws.Range(ws.Cells(1, 1), ws.Cells(1, 1))
cmt = rng.AddComment('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum')
cmt.Shape.TextFrame.Characters().Font.Bold = False
cmt.ShapeRange.ScaleWidth = 3.0
cmt.ShapeRange.ScaleHeight = 3.0

wb.SaveAs(os.path.join(dirpath, outputfile))
excel.Application.Quit()

我认为 ShapeRange.ScaleWidth、ShapeRange.ScaleHeight 是指定评论框缩放比例的方法,但我没有正确设置属性并收到错误。理想情况下,我想自动调整评论框的尺寸。有什么建议吗?

找到两种方法:

cmt.Shape.Width = 300
cmt.Shape.Height = 100

cmt.Shape.TextFrame.AutoSize = True