I have an Excel sheet that pulls data from a worksheet on a server. I created a python program where I can refresh my Excel sheet to include any new data. Once it’s refreshed I should be able to add the new data based on the date it was added to another Excel sheet to import into a database. When I run my program and hit refresh, it refreshes perfectly. The issue is I can’t add the new data to the other Excel sheet unless I close my python program and reopen it. The new data is there, but it’s like it’s locked unless I close the program and rerun it.
def refresh_excel_connections(file_name):
# Create a new instance of Excel
xlapp = win32com.client.DispatchEx("Excel.Application")
try:
# Open the specific workbook
xlwb = xlapp.Workbooks.Open(file_name)
# Refresh all data connections
xlwb.RefreshAll()
# Ensure all asynchronous queries are done calculating
xlapp.CalculateUntilAsyncQueriesDone()
# Save the workbook
xlwb.Save()
finally:
# Close the workbook
xlwb.Close(SaveChanges=True)
# Quit the application
xlapp.Quit()