简体   繁体   English

我应该使用 pyodbc 还是 win32com 使用 python 在 Microsoft Access 数据库 (.accdb) 中填写预先存在的 forms?

[英]Should I use pyodbc or win32com to fill out pre-existing forms in a Microsoft Access Database (.accdb) using python?

I wrote a python script that extracts strings from word documents.我写了一个 python 脚本,从 word 文档中提取字符串。 The goal is to then insert these strings into a pre-existing form in an Access database, save it, create a duplicate of the form, insert different strings from the next word document, save, repeat.目标是然后将这些字符串插入到 Access 数据库中预先存在的表单中,保存它,创建表单的副本,从下一个 word 文档中插入不同的字符串,保存,重复。 The form already has all the fields I need, and it has buttons for 'save' and 'create duplicate.'该表单已经包含我需要的所有字段,并且它具有“保存”和“创建副本”按钮。

I'm having trouble figuring out how to insert strings into the Access form.我无法弄清楚如何将字符串插入 Access 表单。

So far I've learned that there are at least two ways of doing this, using pyodbc or win32com.到目前为止,我已经了解到至少有两种方法可以做到这一点,使用 pyodbc 或 win32com。

I used code from the following links:我使用了以下链接中的代码:

pyodbc - https://datatofish.com/how-to-connect-python-to-ms-access-database-using-pyodbc/ pyodbc - https://datatofish.com/how-to-connect-python-to-ms-access-database-using-pyodbc/

win32com - Write to MS Access table, python win32com win32com - 写入 MS Access 表,python win32com

I solved the problem of connecting 64-bit python to 32-bit access, and I was able to connect to my accdb file using win32com and pyodbc, but that's as far as I got.我解决了将 64 位 python 连接到 32 位访问的问题,并且我能够使用 win32com 和 pyodbc 连接到我的 accdb 文件,但就我所知。

pyodbc: pyodbc:

import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\path\folder\my_database_file.accdb;')
cursor = conn.cursor()

win32com: win32com:

import win32com.client

# ADODB constants
adVarWChar = 202
adInteger = 3
adParamInput = 1

connection = win32com.client.Dispatch(r'ADODB.Connection')

DSN = (
    r'PROVIDER=Microsoft.ACE.OLEDB.12.0;'
    r'DATA SOURCE=C:\path\folder\my_database_file.accdb;'
    )

connection.Open(DSN)

cmd = win32com.client.Dispatch(r'ADODB.Command')
cmd.ActiveConnection = connection

Which approach is better/easier to work with, pyodbc or win32com?哪种方法更好/更容易使用,pyodbc 或 win32com?

Also, once I choose an approach, how do I continue?另外,一旦我选择了一种方法,我该如何继续? I'm having trouble finding documentation on how to fill out forms in Access using these methods.我无法找到有关如何使用这些方法在 Access 中填写 forms 的文档。 Most of what I find was on how to fill out tables in Access, and I'm not sure how to transcribe that code to work with forms instead, or where that code comes from in the first place.我发现的大部分内容是关于如何在 Access 中填写表格,我不确定如何转录该代码以使用 forms 代替,或者该代码最初来自何处。

I did not include the code that extracts strings from word documents, that part works fine, and isn't really part of the question, just background information.我没有包含从 word 文档中提取字符串的代码,这部分工作正常,并不是问题的一部分,只是背景信息。

Any advice or pointers in the right direction would be much appreciated.任何正确方向的建议或指示将不胜感激。

First and foremost, MS Access forms and reports do not store any data but apply visual interactivity with table or query data at runtime.首先,MS Access forms 和报告不存储任何数据,而是在运行时应用与表或查询数据的可视交互。

Secondly, your question shows how MS Access is the multifaceted thing that is both a GUI application and a database.其次,您的问题表明 MS Access 是一个多方面的东西,既是 GUI 应用程序又是数据库。 Because of this your two approaches ( win32com and pyodbc ) are somewhat distinctly different and partially overlapping.因此,您的两种方法( win32compyodbc )有些明显不同并且部分重叠。 Generally, there are two ways to interact with this software:通常,有两种方法可以与此软件进行交互:

Front-End前端

  1. Simply use the installed Microsoft Office software, MSAccess.exe, with all enabled user interface features to design and use its objects: tables, queries, forms, reports, macros, and modules.只需使用已安装的 Microsoft Office 软件 MSAccess.exe 以及所有启用的用户界面功能来设计和使用其对象:表、查询、forms、报告、宏和模块。

  2. Through programming code, interface to the Microsoft Access object library via an external client such as Excel VBA, Python, or any other COM-connected language or API. Through programming code, interface to the Microsoft Access object library via an external client such as Excel VBA, Python, or any other COM-connected language or API.

    In Python, using win32com allows you to access the GUI objects of MS Access which includes forms and reports and any other Access methods like DoCmd.TransferSpreadsheet or Application.ImportXML .在 Python 中,使用win32com允许您访问 MS Access 的 GUI 对象,其中包括 forms 和报告以及任何其他访问方法,如DoCmd.TransferSpreadsheetApplication.ImportXML To run queries you need to access the underlying database of the Access application object.要运行查询,您需要访问 Access 应用程序 object 的底层数据库。 Overall, this approach requires the full-fledged Office application, MSAccess.exe, installed on client machines.总的来说,这种方法需要在客户端计算机上安装成熟的 Office 应用程序 MSAccess.exe。

Back-End后端

  • Connect to the underlying database of the .accdb (or .mdb ) file such as with ODBC or OLEDB using practically any modern, general purpose language like Python with corresponding libraries.使用几乎任何现代通用语言(如 Python)和相应的库连接到.accdb (或.mdb )文件的底层数据库,例如 ODBC 或 OLEDB。

    In Python, using pyodbc (or adodbapi ) only allows you to interact with the Jet/ACE database (Window.dll files).在 Python 中,使用pyodbc (或adodbapi )只允许您与 Jet/ACE 数据库(Window.dll 文件)进行交互。 You cannot interact with any GUI objects including forms and reports but only tables and stored queries and only using SQL called by application layer here being Python.您不能与任何GUI 对象(包括 forms 和报告)进行交互,但只能与表和存储的查询交互,并且只能使用由应用层调用的 SQL,此处为 Python。 Overall, this approach does NOT require the full-fledged Office application, MSAccess.exe, installed on client machines.总的来说,这种方法不需要在客户端计算机上安装成熟的 Office 应用程序 MSAccess.exe。


With that said, for your specific needs, you may not need the longer, more extensive win32com front-end approach since Access forms are designed to enter/update/delete data in underlying tables.话虽如此,对于您的特定需求,您可能不需要更长、更广泛的win32com前端方法,因为 Access forms 旨在输入/更新/删除基础表中的数据。 In other words, they are user-friendly means to data handling.换句话说,它们是用户友好的数据处理方式。 Therefore, simply circumnavigate the user interface need and directly import your extracted Word data into the database table behind the form with pyodbc , a much simpler, back-end method.因此,只需绕过用户界面需求,直接使用pyodbc将提取的 Word 数据导入表单后面的数据库表中,这是一种更简单的后端方法。

Specifically, to answer your questions:具体来说,回答您的问题:

I wrote a Python script that extracts strings from Word documents.我写了一个从 Word 文档中提取字符串的 Python 脚本。 The goal is to then insert these strings into a pre-existing form in an Access database, save it, create a duplicate of the form, insert different strings from the next word document, save, repeat.目标是然后将这些字符串插入到 Access 数据库中预先存在的表单中,保存它,创建表单的副本,从下一个 word 文档中插入不同的字符串,保存,重复。

First, no one should duplicate the actual form object but the data.首先,任何人都不应复制实际表格 object 而是复制数据。 Instead, insert data into the data source behind the form using the cursor object and parameterization:相反,使用cursor object 和参数化将数据插入表单后面的数据源:

# APPEND QUERY WITH PARAMETERS
sql = """INSERT INTO myTableBehindmyForm (Field1, Field2, Field3, ...)
         VALUES (?, ?, ?, ...)
      """

# EXECUTE QUERY WITH TUPLE OF BINDED VALUES
cursor.execute(sql, (word_string1, wordstring2, wordstring3, ...))

The form already has all the fields I need, and it has buttons for 'save' and 'create duplicate.该表单已经包含我需要的所有字段,并且它具有用于“保存”和“创建副本”的按钮。 I'm having trouble figuring out how to insert strings into the Access form.我无法弄清楚如何将字符串插入 Access 表单。

To save, simply commit above query and to duplicate, repeat the cursor.execute call:要保存,只需提交上述查询并复制,重复cursor.execute调用:

# EXECUTE QUERY WITH TUPLE OF BINDED VALUES
cursor.execute(sql, (word_string1, wordstring2, wordstring3, ...))
conn.commit()


# EXECUTE QUERY WITH TUPLE OF BINDED VALUES
cursor.execute(sql, (word_string1, wordstring2, wordstring3, ...))
cursor.execute(sql, (word_string1, wordstring2, wordstring3, ...))
conn.commit()

Most of what I find was on how to fill out tables in Access, and I'm not sure how to transcribe that code to work with forms instead, or where that code comes from in the first place.我发现的大部分内容是关于如何在 Access 中填写表格,我不确定如何转录该代码以使用 forms 代替,或者该代码最初来自何处。

Again, no need to work with forms (data-less GUI objects) but simply the tables behind forms.同样,无需使用 forms(无数据 GUI 对象),只需使用 forms 后面的表即可。 Therefore, go with pyodbc or any compliant MS Access and Python DB-API to handle your data needs.因此,go 与pyodbc或任何兼容的 MS Access 和 Python DB-API 来处理您的数据需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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