How to compact and repair an Access database from a Python script [closed]

拜拜、爱过 提交于 2019-12-02 11:40:55

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  

I know this is old, but just an minor change to the code above to anyone using it in the future (I just had to use this now, thanks Parfait for posting this solution).

The line that states:

oApp = win32com.client.Dispatch("Access.Application")

should be

oApp = win32com.Dispatch("Access.Application")

thanks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!