refresh.py 777 B

12345678910111213141516171819202122232425262728293031
  1. import plac
  2. import win32com.client
  3. from pathlib import Path
  4. @plac.pos('filename', '', type=Path)
  5. def refresh(filename):
  6. excel = win32com.client.Dispatch('Excel.Application')
  7. excel.DisplayAlerts = False
  8. print(f"Refresh '{filename.resolve()}'")
  9. wb = excel.Workbooks.Open(filename.resolve())
  10. try:
  11. for source in wb.LinkSources(1):
  12. print(' --> ' + source)
  13. if Path(source).exists():
  14. excel.Workbooks.Open(source)
  15. else:
  16. print(' !! does not exist !!')
  17. except TypeError:
  18. print(' --> ' + 'no LinkSources')
  19. wb.RefreshAll()
  20. wb.Save()
  21. wb.Close(True)
  22. excel.Application.Quit()
  23. if __name__ == '__main__':
  24. plac.call(refresh)