import plac
import win32com.client
from pathlib import Path


@plac.pos('filename', '', type=Path)
def refresh(filename):
    excel = win32com.client.Dispatch('Excel.Application')
    excel.DisplayAlerts = False

    print(f"Refresh '{filename.resolve()}'")
    wb = excel.Workbooks.Open(filename.resolve())

    try:
        for source in wb.LinkSources(1):
            print(' --> ' + source)
            if Path(source).exists():
                excel.Workbooks.Open(source)
            else:
                print('     !! does not exist !!')
    except TypeError:
        print(' --> ' + 'no LinkSources')

    wb.RefreshAll()
    wb.Save()
    wb.Close(True)
    excel.Application.Quit()


if __name__ == '__main__':
    plac.call(refresh)
