我已经用Python 3创建了一个Excel (.xlsx)文件。现在我想把这个 Excel 文件转换成 Access (.accdb) 文件。我知道 Access 可以导入 Excel 文件,但我想用 Python 来自动完成。
在Excel文件中,有3个工作表。我已经在Excel和Access之间建立了连接,但不知道如何在Access文件中插入这些表/值。非常感谢您的帮助!非常感谢!
writer=pd.ExcelWriter('ETABS.xlsx',engine='xlsxwriter')
pointcord.to_excel(writer, sheet_name='Sheet1')
jointreaction.to_excel(writer, sheet_name='Sheet2')
writer.save()
import pyodbc
pyodbc.drivers()
DBFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.accdb'
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBFile)
exFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.xlsx'
conn1 = pyodbc.connect('DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ='+exFile,autocommit=True)
curs1 = conn1.cursor()
# the following returns list of tuples
excel_results = curs1.execute().fetchall()
curs.executemany("INSERT INTO ETABS values (?,?)", excel_results)
conn.commit()
for row in curs1.tables():
print (row.table_name)