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,
Ben.
解决方案
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