How to find all Excel files that have an SQL Connection string?

I’ve been tasked to do an audit of all the company wide Excel files, that could possibly be using SQL Server connections to source data? This is to determine the scope of work required to convert legacy connection strings (ODBC) over to a stronger and encrypted framework, of which I am still researching to determine what is best practice here.

I tried Python using this script, but I am constantly getting “none” found for a connection string, but I know there is one in a sample XLSX file I’ve been using to test.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
import glob
import zipfile
from lxml import etree
def list_excel_files(folder_path):
return glob.glob(os.path.join(folder_path, "*.xlsx"))
def extract_connection_string_from_custom_xml(file_path):
with zipfile.ZipFile(file_path, 'r') as z:
for item in z.namelist():
if item.startswith('connection'):
xml_content = z.read(item)
tree = etree.XML(xml_content)
conn_string = search_for_connection_string(tree)
if conn_string:
return conn_string
return None
def search_for_connection_string(tree):
connection_elements = tree.xpath("connection")
for elem in connection_elements:
parent = elem.getparent()
if parent is not None and parent.tag == 'cell':
return parent.text
return None
def main(folder_path):
excel_files = list_excel_files(folder_path)
connection_strings = {}
for file_path in excel_files:
conn_string = extract_connection_string_from_custom_xml(file_path)
connection_strings[file_path] = conn_string
return connection_strings
# Example usage:
folder_path = "G:\test\"
connection_strings = main(folder_path)
for file_path, conn_string in connection_strings.items():
print(f"File: {file_path}nConnection String: {conn_string}n")
</code>
<code>import os import glob import zipfile from lxml import etree def list_excel_files(folder_path): return glob.glob(os.path.join(folder_path, "*.xlsx")) def extract_connection_string_from_custom_xml(file_path): with zipfile.ZipFile(file_path, 'r') as z: for item in z.namelist(): if item.startswith('connection'): xml_content = z.read(item) tree = etree.XML(xml_content) conn_string = search_for_connection_string(tree) if conn_string: return conn_string return None def search_for_connection_string(tree): connection_elements = tree.xpath("connection") for elem in connection_elements: parent = elem.getparent() if parent is not None and parent.tag == 'cell': return parent.text return None def main(folder_path): excel_files = list_excel_files(folder_path) connection_strings = {} for file_path in excel_files: conn_string = extract_connection_string_from_custom_xml(file_path) connection_strings[file_path] = conn_string return connection_strings # Example usage: folder_path = "G:\test\" connection_strings = main(folder_path) for file_path, conn_string in connection_strings.items(): print(f"File: {file_path}nConnection String: {conn_string}n") </code>
import os
import glob
import zipfile
from lxml import etree


def list_excel_files(folder_path):
    return glob.glob(os.path.join(folder_path, "*.xlsx"))


def extract_connection_string_from_custom_xml(file_path):
    with zipfile.ZipFile(file_path, 'r') as z:
        for item in z.namelist():
            if item.startswith('connection'):
                xml_content = z.read(item)
                tree = etree.XML(xml_content)
                conn_string = search_for_connection_string(tree)
                if conn_string:
                    return conn_string
    return None


def search_for_connection_string(tree):
    connection_elements = tree.xpath("connection")
    for elem in connection_elements:
        parent = elem.getparent()
        if parent is not None and parent.tag == 'cell':
            return parent.text
    return None


def main(folder_path):
    excel_files = list_excel_files(folder_path)
    connection_strings = {}

    for file_path in excel_files:
        conn_string = extract_connection_string_from_custom_xml(file_path)
        connection_strings[file_path] = conn_string

    return connection_strings


# Example usage:
folder_path = "G:\test\"
connection_strings = main(folder_path)

for file_path, conn_string in connection_strings.items():
    print(f"File: {file_path}nConnection String: {conn_string}n")

The area I think I am stuck with here is how to find an actual “connection” keyword in the XML result. However, I get a feeling there’s different flavours of connection strings out there as some Excel files are over 10 years old and they most likely have used legacy ODBC 32 bit DNS strings? Not sure yet hence why I am asking.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật