user145
user145

Reputation: 17

Saving an already open Excel window with python

I have been having issues finding an answer for this one. I am currently utilizing win32com, but while it is very powerful, I cannot figure out how to tap into an Excel file that's already open.

There is no alternative ; an external script opens Excel and writes to it the data, it is not located on the hard disk

Upvotes: 2

Views: 4556

Answers (2)

a_ko
a_ko

Reputation: 159

I haven't got enough reputation to upvote Maxime Biette's answer but it's the right one.

If you just want one specific sheet to be saved:

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    if wb.Name == 'thenameofyourfile.xlsx' :

        print("WB:",wb.Name)
        wb.Save()

Upvotes: 2

Maxime Biette
Maxime Biette

Reputation: 303

You can access all the opened workbooks by looping through Excel.Application.Workbooks:

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    print("WB:",wb.Name)
    wb.Save()

Upvotes: 4

Related Questions