简体   繁体   中英

Python 3.0 Import Excel file to Access File

I have used Python 3 to create an Excel (.xlsx) file. Now I want to convert this Excel file to an Access (.accdb) file. I know Access can import Excel file, but I am trying to use Python to automate this.

There are 3 sheets in the Excel file. I have made a connection between Excel and Access but not sure how to insert the sheets/values in the Access file. Thank you so much for your help! Many Thanks!

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)

Consider pure SQL as the JET/ACE engine allows querying external workbooks and databases directly. You can do so from either an MS Access connection or Excel connection since same underlying engine is used. No need for row by row cursor append.

Below assumes ALL Excel worksheet columns matches one-to-one with ALL Access table columns in same order. If you use an autonumber field in Access table, consider explicitly stating columns in INSERT INTO and SELECT clauses of append query.

Access Connection (local DB table append)

DBFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.accdb'
exFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.xlsx'

conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBFile)

# APPEND TO LOCAL TABLE
sql = """INSERT INTO ETABS
         SELECT * FROM [Excel 12.0 Xml; HDR = Yes;Database={myfile}].[Sheet1$];
      """

curs = conn1.cursor()
curs.execute(sql.format(myfile = exFile))
conn.commit()

By the way, you can even run a make-table query instead of appending to an existing table:

SELECT * INTO myNewTable FROM [Excel 12.0 Xml; HDR=Yes; Database={myfile}].[Sheet1$]

Excel Connection (external DB table append)

DBFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.accdb'
exFile = r'C:\Users\nyeung\Documents\wsp.codingworkshop.python\ClassNotebooks\ETABS.xlsx'

conn = pyodbc.connect('DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ='+exFile)
curs1 = conn1.cursor()

# APPEND TO EXTERNAL TABLE
sql = """INSERT INTO [{myfile}].[ETABS]
         SELECT * FROM [Sheet1$];
      """

curs = conn.cursor()
curs.execute(sql.format(myfile = DBFile))
conn.commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM