Код: Выделить всё
Application = Dispatch("Excel.Application")
#Application.DisplayAlerts = False
#opens the excel file
wb = Application.Workbooks.Open(path)
if wb.ReadOnly:
wb.Close(False)
try:
os.system("taskkill /f /im excel.exe")
time.sleep(5)
except:
pass
return True
#if variables passed in then replace in query string
if query_list != []:
for query_name in query_list:
#existing query is initialized
current_q = wb.Queries(f"{query_name}").Formula
#replace string requires the new string as well as the old string to be replaces
new_q = current_q.replace(old_str,replacement_str)
wb.Queries(f"{query_name}").Formula = new_q
#refresh
wb.RefreshAll()
Application.CalculateUntilAsyncQueriesDone()
if sub_list != []:
for sub_name in sub_list:
Application.Run("sub_name")
#Updates the Index tab if required
if index_update:
wb.Sheets("Index").Range("E5").Value = f"Last Updated: {datetime.now().strftime('%d/%m/%Y')}"
if max_bk_date != '':
wb.Sheets("Index").Range("E6").Value = f"Last Booking Date Updated: {max_bk_date}"
#Save & Close
wb.Close(True)
try:
os.system("taskkill /f /im excel.exe")
time.sleep(5)
except:
pass
return False
Пробовал wb.Queries(f"{query_name}").Refresh(), но выдает следующую ошибку
wb.Queries( query_name).Refresh() Файл «», строка 2, в Refresh pywintypes.com_error: (-2147352567, «Произошло исключение.», (0, Нет, Нет, Нет, 0, -2146827284), Нет)< /p>
ps: я использую соединение для запроса с SQL-сервера
Подробнее здесь: https://stackoverflow.com/questions/790 ... g-win32com