0

I am trying to add an Excel Textbox to a worksheet... the typical shortcut I use in the Excel GUI is Alt+N X and then click where I want the Textbox; however, I don't have access to the COM browser, which leaves me guessing where Microsoft hid the Textbox API under Python's win32com...

from win32com import client

excel=client.Dispatch("Excel.Application")
excel.Visible=True
book=excel.Workbooks.Open("c:/Users/dpennington/Desktop/Blank.xls", False, 
    True)
sheet=book.Worksheets(2)

How would I add a textbox (i.e. in the Excel GUI: Alt+N X), using Python's win32com api? (Specific positioning in the worksheet is up to you...)

3
  • Try openpyxl instead: pythonhosted.org/openpyxl if possible? or any of the following for xls files: python-excel.org Commented May 13, 2013 at 20:03
  • @Torxed, if you post an answer that works using openpyxl, I will accept that... win32com is not required, but I could not find the right API with openpyxl Commented May 13, 2013 at 20:10
  • @Mike_pennington Wasn't sure my answer really answered your question completely since i'm afraid i wasn't really sure what you mean by "Textbox"? Commented May 13, 2013 at 20:18

1 Answer 1

2

Use the AddTextbox method of the Shapes object:

import win32com.client as client

xl = client.Dispatch("Excel.Application")
xl.Visible = True
wb = xl.Workbooks.Open("c:/1temp/badacres.xls")
ws = wb.Sheets(1)

tb = ws.Shapes.AddTextbox(1, 570, 45, 171, 80)
tb.TextFrame2.TextRange.Characters.Text = 'This is a great big test.'

You can find more on the AddTextbox method here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.