dropdown menu

Back To Home

Monday, June 29, 2020

Python - Read open excel file data

Sometimes we want to read real time update data for excel that haven't save.
So below code is useful.


Python code:

import win32com.client as win32
import pandas as pd

#here we use win32com to save open excel
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
    if wb.Name == 'da.xlsx' :

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

#here we use panda module to read and check
df = pd.read_excel (r'da.xlsx', sheet_name='Sheet1')
print (df)