首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python保存MS ACCESS附件

使用python保存MS ACCESS附件
EN

Stack Overflow用户
提问于 2018-07-02 04:25:56
回答 2查看 471关注 0票数 1

这看起来很简单,但事实并非如此,我们的目标是创建一个带有表单的离线数据库,这样用户就可以填充数据,然后将这些数据放入报告中。问题是这个报表有复杂的格式和图片,所以我的想法是从MS ACCESS中提取数据(可以很好地创建管理附件的表格和表单),并使用doctpl将其插入到word模板中。

我的问题是,我不能设法在访问表中提取附加的图片。我尝试过这样使用win32com.client:

代码语言:javascript
复制
import win32com.client
daoEngine = win32com.client.Dispatch('DAO.DBEngine.120')
db = r"C:\Users\P\Documents\db.accdb"
daoDB = daoEngine.OpenDatabase(db)
query = "SELECT picture FROM Galery WHERE ID=13"
daoRS = daoDB.OpenRecordset(query,2)
daoRS.Edit()
daoRS.Fields["picture"].SaveToFile("C:\Users\PCA037\Documents\\")
daoDB.Close()

但它返回错误:

代码语言:javascript
复制
(-2147352567, 'Exception occurred.', (0, u'DAO.Field', u'Invalid field data type.', u'jeterr40.chm', 5003259, -2146825029), None)

有没有人能帮我一下?

或者,我知道我不被允许征求意见,但是有没有人知道更好的方法来实现一个离线数据库,具有“漂亮”的表单和附件管理,可以轻松访问?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-02 05:13:48

附件文件数据存储在字段数据内的子记录集中。

您需要首先访问该子记录集,然后对该子记录集调用.SaveToFile,而不是对主记录集调用:

代码语言:javascript
复制
import win32com.client
daoEngine = win32com.client.Dispatch('DAO.DBEngine.120')
db = r"C:\Users\P\Documents\db.accdb"
daoDB = daoEngine.OpenDatabase(db)
query = "SELECT picture FROM Galery WHERE ID=13"
daoRS = daoDB.OpenRecordset(query,2)
daoAttachmentRS = daoRS.Fields["picture"].Value
daoAttachmentRS.Fields["FileData"].SaveToFile("C:\Users\PCA037\Documents")
daoDB.Close()

文件数据始终保存在名为FileData的字段中。还存在其他字段,如附件类型、标志和文件名,并且单个记录可以存在多个附件(目前,此代码保存第一个附件,如果没有,则抛出错误,并忽略所有其他附件。可以对子记录集使用.EOF.MoveNext来检查更多附件并保存它们。

票数 1
EN

Stack Overflow用户

发布于 2018-07-25 22:54:48

正如Erik所指出的,使用指向文件的完全限定路径将图像保存为附件;不要试图在数据库中保存实际的图像。

https://support.office.com/en-us/article/attach-files-and-graphics-to-the-records-in-your-database-d40a09ad-a753-4a14-9161-7f15baad6dbd

https://www.cimaware.com/expert-zone/working-with-attachment-data-type-in-microsoft-access

与附件相关的重要信息:

代码语言:javascript
复制
The maximum size of an attached data piece cannot be larger than 256Mb.

Adding, deleting, and editing of the attached items is only possible through an Attachments dialog box.

In the design of Forms and Reports there is new item in the toolbox: the Attachment control (looks like a paper clip). This control may be used at design time.

Editing an attachment is possible if the program used for creating the attachment is available on the local computer. The program will edit the attachment and when it is saved the attachment gets saved to its own field.

Access will compress uncompressed files in the attachments before storing them.

Attachments may originate from any location on the disk drive or the network.

VBA can be used to work with attachments programmatically using the new Attachment Object. It has several properties and methods, and it supports events.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51126769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档