用Python运行Excel文件的Demo

python文件

# -*- coding:utf-8 -*-

'''
author: lipeijie_1
'''

import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
bk = excel.Workbooks.Open(os.path.join(os.getcwd(), 'test.xlsm'))
# bk.Worksheets(1).Cells(1,1).Value = 'test'
strPara = bk.Name + '!bb("this is a msgbox from python!")'
status = excel.ExecuteExcel4Macro(strPara)
print(status)
bk.Save()
bk.Close()
excel.Application.Quit()

xlsm文件

Sub bb(str)
     MsgBox str
End Sub

 

posted @ 2017-02-09 17:07  ryan1985  阅读(639)  评论(0编辑  收藏  举报