Removing Existing Excel Connections with Python

Asked

Viewed 45 times

0

I need help to remove the Excel connection, where I have an SQL query inside Excel that feeds several tabs that are inside Excel, but when I use the code below returns me with an error

conn.Delete()
File "<COMObject <unknown>>", line 2, in Delete

The code I’m using is

import win32com.client

Bases = ["customer1","customer2","customer3"]

fileDest = '//caminho/do/arquivo/final/sem/conexao'

for base in Bases:
    dir = '//caminho/dos/arquivos/com/conexao{}'.format(base)
    print(base)
    xlapp = win32com.client.DispatchEx("Excel.Application")
    wb = xlapp.Workbooks.Open(dir)
    xlapp.Visible = True
    wb.RefreshAll()
    wb.Save()    
    print(base,' Atualizou')
    print('\n')
    print('removendo conexao') 
    for conn in wb.Connections: # quando chega aqui ele da erro
        conn.Delete()
    print('removido')
wb.SaveAs(fileDest)
xlapp.Quit()
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.