Mike Mueller
Mike Mueller

Reputation: 21

Python - Control/Edit existing window/workbook of Excel

I'm working on a little script to automate some data entry. I have two windows open that this same data needs to be entered in every 15 minutes or so. One program I have it entering through GUI manipulation with pywinauto. The other window is an Excel workbook.

My problem is, the workbook is and must be left open (it displays a chart I use and analyze in real-time). I'm wondering how I can go about having my Python script find that specific window (the file name and location is known) without having to open a new instance. I need to edit 4 cells on a specific Worksheet on this Workbook.

Is this possible (to edit/control an existing Workbook that is already open in Windows)?

Upvotes: 2

Views: 1504

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 10000

Yes, it's possible. You can use Workbooks property of Excel.Application object:

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
for w in excel.Workbooks:
    print(w.Name)

You should see "Book1" printed, the name of the new file open. Then run this command:

excel.Workbooks[1].Worksheets[1].Cells[1][1].Value = '444'

You should now see cell A1 have the value of 444.

Upvotes: 0

Related Questions