简体   繁体   中英

How do I transfer data from Excel>Python>Microsft Access

I have been working on this for days now. Someone please give any advice that you can. Here is my current code (it doesn't work, and I know why it doesn't work):

import pyodbc

import openpyxl

path = ('C:\\Access_Test.xlsx')
wb = openpyxl.load_workbook(path)
sheet = wb.active
b2 = a2 = sheet['A2']
b2 = sheet['B2']
c2 = sheet['C2']
d2 = sheet['D2']
e2 = sheet['E2']
f2 = sheet['F2']
g2 = sheet['G2']
h2 = sheet['H2']
i2 = sheet['I2']
j2 = sheet['J2']
k2 = sheet['K2']
l2 = sheet['L2']
m2 = sheet['M2']
n2 = sheet['N2']
o2 = sheet['O2']
test2 = (")'")
test =  (a2.value, b2.value, c2.value, d2.value, e2.value, f2.value, g2.value, h2.value, i2.value, j2.value, k2.value, l2.value, m2.value),(test2)  

#Everything to this point is fine.  I can read & print everything from the Excel document (though the formatting is an issue with how the query statements work in pyodbc).




driver = '{Microsoft Access Driver(*.mdb, *accdb)}'
filepath = 'C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\HUD Report-2001-Copy For Python.mdb'

#Find data sources
myDataSources = pyodbc.dataSources()
access_driver = myDataSources['MS Access Database']

#This is the full command to open the Access database
cnxn = pyodbc.connect(driver=access_driver, dbq=filepath, autocommit=True)
crsr = cnxn.cursor()

crsr.execute(str(test))

If I use:

print:(test)

My output looks like this (all data is fake for this test):

("'''INSERT INTO Python_Test([Case2], [Last], [First], [Initial Intake], [Intake], [Age], [Gender], [Ethnic], [Race], [DOB], [SSN], [Educ Lvl], [Marital])VALUES", ('Sep00000', 'Test', 'Test', '01/01/2020', '01/01/2020', 1, 'Male', 'A. Hispanic', 'E. White', '01/01/2020', 0, 'High School'), ")'")

As you can see, this is about 70% correct for the purpose of pyodbc, but It obviously throws back errors (too many quotation marks at the beginning, quotation mark after "VALUES", the " " after "VALUES".... etc, you get my point). Is anyone capable of trying to explain how to make this code functional?

Removing the extra quotes at the beginning isn't the biggest issue necessarily, I think I can figure that out; but everything going on after the "VALUES" portion is just a huge mess.

Would appreciate ANY feedback!

I'm guessing you just need to properly format a string to be valid SQL query. Try something like this

sql = f"INSERT INTO table([Case2], [Last], ...) VALUES ({a2.value}, {b2.value}, ...)"

or

sql = ''.join(test)

Here is the working code guys!

import pyodbc

import openpyxl

path = ('C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\Access_Test.xlsx') #Set the path to the Excel document that you want to transfer data from
wb = openpyxl.load_workbook(path)
sheet = wb.active


b2 = sheet['B2']
c2 = sheet['C2']
d2 = sheet['D2']
e2 = sheet['E2']
f2 = sheet['F2']
g2 = sheet['G2']
h2 = sheet['H2']
i2 = sheet['I2']
j2 = sheet['J2']
k2 = sheet['K2']
l2 = sheet['L2']
m2 = sheet['M2']
n2 = sheet['N2']
o2 = sheet['O2']

#This is the trouble spot.  If you've ever worked with this stuff you know that the formatting has to be PERFECT.  A single space out of place throws errors. 

startcmmd = "'''INSERT INTO Python_Test([Case2], [Last], [First], [Initial Intake], [Intake], [Age], [Gender], [Ethnic], [Race], [DOB], [SSN], [Educ Lvl], [Marital])VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}')'''".format(b2.value, c2.value, d2.value, e2.value, f2.value, g2.value, h2.value, i2.value, j2.value, k2.value, l2.value, m2.value, n2.value)

#Get connected to your Access document

driver = '{Microsoft Access Driver(*.mdb, *accdb)}'
filepath = 'C:\\Users\\Db_Mngr\\Desktop\\PythonTests\\Microsoft Studio Projects\\HUD Report-2001-Copy For Python.mdb'


myDataSources = pyodbc.dataSources()
access_driver = myDataSources['MS Access Database']

#set up your cursor    

cnxn = pyodbc.connect(driver=access_driver, dbq=filepath, autocommit=True)
crsr = cnxn.cursor()

#Now execute!  Don't forget to run this with eval!
crsr.execute(eval(startcmmd))

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