python运行excel的VBA代码

import os, os.path
import win32com.client
    
if os.path.exists("ceshi.xlsm"):
    xl=win32com.client.Dispatch("Excel.Application")
    wb = xl.Workbooks.Open(os.path.abspath("ceshi.xlsm"), ReadOnly=1) #打开excel文件,只读模式
    xl.Application.Run("ceshi.xlsm!Module1.ceshi") #运行VBA代码
    wb.Close(False) #close the work sheet object rather than quitting excel
    del wb
    del xl

  python运行excel的VBA代码;非常简单。

posted @ 2022-07-12 21:00  uuxyz  阅读(409)  评论(0)    收藏  举报
联系我