Cy Bu
Cy Bu

Reputation: 1437

Xlwings: AddComment generates a Text pywintypes.com_error

Windows 10, Python 3.6, xlwings 0.24.9

Example:

xlsx_book = xw.books.active
sht = xlsx_book.sheets['this tab']

sht.range('B5').value = 3
sht.range('B5').api.AddComment('model #3')

Although the comments gets inserted, the code still throws the following:

Traceback (most recent call last): File "C:\Users\xxx\OneDrive - xxx.py", line xxx, in sht.range('B5').api.AddComment('model #3') File "C:\Users\xxx\AppData\Roaming\Python\Python36\site-packages\xlwings_xlwindows.py", line 70, in call v = self.__method(*args, **kwargs) File "C:\Users\xxx\AppData\Local\Temp\gen_py\3.6\00020813-0000-0000-C000-000000000046x0x1x9.py", line 33496, in AddComment ret = self.oleobj.InvokeTypes(1389, LCID, 1, (9, 0), ((12, 17),),Text pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)

Any lead what is happening?

Upvotes: 0

Views: 273

Answers (1)

mouwsy
mouwsy

Reputation: 1933

You can try using .api.Comment.Text():

import xlwings as xw

wb = xw.Book()
ws = wb.sheets[0]
ws.range('A1').api.AddComment()
ws.range('A1').api.Comment.Text('Some Text')

Note that it is not possible to use ws.range('A1').api.AddComment() if there is already a comment assigned in the cell in question (in this case A1). If this is the case you would have to use only ws.range('A1').api.Comment.Text('Some Text') or ws.range('A1').note.text = 'Some Text'.

Upvotes: 2

Related Questions