python讀取mdb文件數(shù)據(jù),并保存為excel

問題: 有mdb文件,但本地沒有access無法打開,就用python 將它轉(zhuǎn)為excel;
環(huán)境及相關(guān)的包:pyodbc, openpyxl
代碼:
import pyodbc
import pandas as pd

# MDB 文件路徑
mdb_path = r'D:\test.mdb'

# 連接字符串
connection_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=' + mdb_path

# 創(chuàng)建數(shù)據(jù)庫連接
connection = pyodbc.connect(connection_string)

# 獲取所有表的信息
cursor = connection.cursor()
tables = cursor.tables(tableType='TABLE')

# 打印表名
for table in tables:
    print(table.table_name)

# 查詢數(shù)據(jù)
query = 'SELECT * FROM table'
data_frame = pd.read_sql(query, connection)
print(data_frame)

# 關(guān)閉數(shù)據(jù)庫連接
connection.close()

# 指定輸出excel文件路徑
excel_path = r'D:\a.xlsx'
# 將Dataframe 寫入excel文件
data_frame.to_excel(excel_path, index=False)
print(f'數(shù)據(jù)已成功保存到 {excel_path}')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容