这看起来很简单,但事实并非如此,我们的目标是创建一个带有表单的离线数据库,这样用户就可以填充数据,然后将这些数据放入报告中。问题是这个报表有复杂的格式和图片,所以我的想法是从MS ACCESS中提取数据(可以很好地创建管理附件的表格和表单),并使用doctpl将其插入到word模板中。
我的问题是,我不能设法在访问表中提取附加的图片。我尝试过这样使用win32com.client:
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()
但它返回错误:
(-2147352567, 'Exception occurred.', (0, u'DAO.Field', u'Invalid field data type.', u'jeterr40.chm', 5003259, -2146825029), None)
有没有人能帮我一下?
或者,我知道我不被允许征求意见,但是有没有人知道更好的方法来实现一个离线数据库,具有“漂亮”的表单和附件管理,可以轻松访问?
发布于 2018-07-02 05:13:48
附件文件数据存储在字段数据内的子记录集中。
您需要首先访问该子记录集,然后对该子记录集调用.SaveToFile,而不是对主记录集调用:
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
来检查更多附件并保存它们。
发布于 2018-07-25 22:54:48
正如Erik所指出的,使用指向文件的完全限定路径将图像保存为附件;不要试图在数据库中保存实际的图像。
https://www.cimaware.com/expert-zone/working-with-attachment-data-type-in-microsoft-access
与附件相关的重要信息:
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.
https://stackoverflow.com/questions/51126769
复制相似问题