如何从 Excel>Python>Microsoft Access 传输数据

Posted

技术标签:

【中文标题】如何从 Excel>Python>Microsoft Access 传输数据【英文标题】:How do I transfer data from Excel>Python>Microsft Access 【发布时间】:2020-09-18 21:16:40 【问题描述】:

我已经为此工作了好几天了。有人请给你任何建议。这是我当前的代码(它不起作用,我知道它为什么不起作用):

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))

如果我使用:

print:(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'), ")'")

如您所见,对于 pyodbc 而言,这大约是 70% 的正确率,但它显然会抛出错误(开头的引号太多,“VALUES”后面的引号,“VALUES”后面的“”。 ...等等,你明白我的意思)。有没有人能够尝试解释如何使这段代码起作用?

删除开头的多余引号不一定是最大的问题,我想我可以解决这个问题;但是在“VALUES”部分之后发生的一切都是一团糟。

如果有任何反馈,我们将不胜感激!

【问题讨论】:

请提供minimal reproducible example。 test 是一个元组,对吧?这不是预期的结果吗? 【参考方案1】:

我猜您只需要正确格式化字符串即可成为有效的 SQL 查询。试试这样的

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

sql = ''.join(test)

【讨论】:

伙计,我周一一上班就去试试。如果 F 命令确实进行了文字输入(我对 Python 还是有点陌生​​),那么您将完成我的一周。 所以要么我的经验不足,要么 f 字符串的功能不起作用,但它确实为我指明了正确的道路(顺便说一句,我倾向于缺乏经验)。我最终采用了 .format 方法,它运行良好。现在将发布工作代码。 更新:这是我的经验不足。 f string & .format 显然基本上做同样的事情。无论出于何种原因,我都更快地了解了如何使用 .format 方法,这就是为什么我更快地发现它的精确格式。感谢卢卡斯的帮助!你真的成就了我的一周!【参考方案2】:

这是工作代码,伙计们!

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))

【讨论】:

以上是关于如何从 Excel>Python>Microsoft Access 传输数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Excel 中使用带查询的 Access 数据库作为数据透视表

如何使用python脚本自动将数据从访问中提取到excel中

怎样把其中的汉字提取出来呀,求Python大佬解答

SQL 如何从日期中提取时间到 HH:MI:SS PM/AM?

excel的导出

如何在 Python 中从流(不是磁盘支持的文件)中读取 Excel 文件?