3

I have an Excel sheet with PowerQuery connecting via ODBC to SAP Data Source.

Usually, I manually click "Refresh All" to refresh data. To automate this, I wrote a Python script to refresh the sheet automatically. It has some issues.

Is there a way to execute this Refresh All command from command line?

Code tried in Python:

 xlapp = win32com.client.DispatchEx("Excel.Application")
 wb = xlapp.workbooks.open(File1.xlsx)
 wb.RefreshAll()
wb.SaveAs(File2.xlsx)
8
  • From within VBA you can refresh all conections using ActiveWorkbook.RefreshAll. Are you using xlwings in python? Commented Aug 5, 2019 at 12:41
  • You should show your code, so we can help you with this. Furthermore "It has some issues" is no useful error description. Commented Aug 5, 2019 at 13:09
  • @M.Getrost No, I did not use XLWings. I just created a WorkBook object and executed something like: wb.Refresh. It is not refreshing. Commented Aug 5, 2019 at 13:09
  • 1
    Yes. check out xlwings. It's an python package. you can refresh your connections with something like this: import xlwings as xw app = xw.App() wb = xw.Book('pathtoFile') wb.api.RefreshAll() Commented Aug 5, 2019 at 13:13
  • 1
    @Pᴇʜ Added code. Commented Aug 5, 2019 at 13:14

2 Answers 2

2

You can use xlwings in python. It's probably the best Excel-Library in Python. your code could look like this:

import xlwings as xw

app = xw.App()

wb = xw.Book('pathToFile')
wb.api.RefreshAll()

Another option, as PEH mentioned, would be to use a VBScript. Open an txt-Editor copy in this Code:

Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
Set ObjWB = ObjExcel.Workbooks.Open("pathToFile")
ObjWB.RefreshAll

Set ObjWB = Nothing
Set ObjExcel = Nothing

save as fileName.vbs Run the VBScript to update the connections.

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

Comments

0

I wanted to add some extra notes for any future visitors.

I ended up using the above from MGP's post and added a few extras.


import xlwings as xw

file = '\\\\networkshare\\folder\\folder\\my excel file with spaces.xlsx'

app = xw.App()

wb = xw.Book(file)

wb.api.RefreshAll()

# Save the file
wb.api.Save()

# Close the file
wb.close()

# Close Excel 
app.kill()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.