How To Insert Cell Comments Using xlwings?

Method

#xlwings API

sht.api.Range(‘A3′).AddComment(Text=’Cell comments’)

 

#Check if cell A3 has a comment

if sht.api.Range(‘A3’).Comment is None:

print(‘Cell A3 has no comment.’)

else:

print(‘Cell A3 has a comment.’)

 

#Hide a comment in cell A3

sht.api.Range(‘A3’).Comment.Visible=False

 

#Delete a comment in cell A3

sht.api.Range(‘A3’).Comment.Delete()

Sample Code

#Cell comments

import xlwings as xw    #Import the xlwings package

app=xw.App()
bk=app.books.active    #Get the active workbook
sht=bk.sheets.active    #Get the active worksheet

#xlwings API
sht.api.Range('A3').AddComment(Text='Cell comments')

#Check if cell A3 has a comment
if sht.api.Range('A3').Comment is None:
    print('Cell A3 has no comment.')
else:
    print('Cell A3 has a comment.')

#Hide a comment in cell A3
sht.api.Range('A3').Comment.Visible=False

#Delete a comment in cell A3
sht.api.Range('A3').Comment.Delete()

#bk.close()
#app.kill()

February 6, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *