Search code examples
excelpywin32xlwings

Change Button text in excel using xlwings/pywin32


I create a FormControl button using shape=ws.shapes.api.AddFormControl(0,1,1,1,1).

The button name can be changed shape.Name='new_name'.

How to change the text on the button? I have tried the following, none of them works.

shape.Caption = new_text'

shape.Text='new_text'

shape.TextFrame.Characters.Text='new_text'


Solution

  • I expect the first 0 is 'xlButtonControl'
    You can set the set the text on the button like so

    shape = ws.shapes.api.AddFormControl(xlButtonControl, 50, 50, 200, 30)
    shape.OLEFormat.Object.Text = "Text on Button"