如何通过Python脚本压缩和修复Access数据库 [英] How to compact and repair an Access database from a Python script
问题描述
我有一个从ArcGIS自定义工具箱运行的python脚本.该脚本处理Access数据库(主要是个人地理数据库).我想知道的是,有没有一种方法可以通过我的python脚本压缩和修复访问数据库(特别是个人gdb)?
I have a python script running from an ArcGIS custom toolbox. The script deals with Access databases (mostly personal geodatabases). What I am wondering is, is there a way to compact and repair an access database (specifically a personal gdb) from my python script?
在此先感谢您的帮助和建议,
Thanks in advance for any help and advice,
本.
推荐答案
使用Python的 win32com 库和调用VBA方法 CompactRepair().请注意,在此过程中需要备份文件路径(尚不存在),但在成功压缩后可以将其删除,为此需要使用os.remove()
Use Python's win32com library and make a call to the VBA method CompactRepair(). Do note, a backup file path is required (one that does not exist yet) during the process but can be deleted after successful compact, using os.remove()
for that need:
import os
import win32com.client
srcDB = 'C:\\Path\\To\\Database.accdb'
destDB = 'C:\\Path\\To\\Database_backup.accdb'
oApp = win32com.client.Dispatch("Access.Application")
oApp.compactRepair(srcDB, destDB)
os.remove(destDB)
oApp = None
这篇关于如何通过Python脚本压缩和修复Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!