Python - 运行Excel宏 [英] Python - run Excel macro

查看:744
本文介绍了Python - 运行Excel宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python在Data.csv的工作表上运行MacroBook.xlsm中包含的宏。

I would like to use Python to run a macro contained in MacroBook.xlsm on a worksheet in Data.csv.

通常在excel中,我有两个文件打开并将焦点移动到Data.csv文件,并从MacroBook运行宏。 python脚本每天下载Data.csv文件,所以我无法将宏放在该文件中。

Normally in excel, I have both files open and shift focus to the Data.csv file and run the macro from MacroBook. The python script downloads the Data.csv file daily, so I can't put the macro in that file.

这是我的代码:

import win32com.client
import os
import xl

excel = win32com.client.Dispatch("Excel.Application")

macrowb = xl.Workbook('C:\MacroBook.xlsm')
wb1 = xl.Workbook('C:\Database.csv')
excel.Run("FilterLoans")

我收到错误,pywintypes.com_error:( -2147352567,异常发生,(0,u'Microsoft Excel',u无法运行宏FilterLoans。宏可能在此工作簿中不可用或所有宏可能被禁用。,u'xlmain11。 chm',0,-2146827284),无)

I get an error, "pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Cannot run the macro 'FilterLoans'. The macro may not be available in this workbook or all macros may be disabled.", u'xlmain11.chm', 0, -2146827284), None)"

错误指出FilterLoans在Database.csv文件中不可用...如何导入?

The error states that FilterLoans is not available in the Database.csv file...how can I import it?

推荐答案

1)您不能在* .csv文件上拥有VBA。您需要将* .xlsm文件作为活动工作簿。如果您的宏知道如何找到它,我不认为你需要打开* .csv文件。

1) you can't have VBA on a *.csv file. You need the *.xlsm file to be the active workbook. I don't think you need to open the *.csv file at all if your macro know how to find it.

2)在Office Excel中启用VBA模块访问:

2) Enable VBA module access in your Office Excel:

File
options
Trust Center
Trust Center Settings
Macro Settings
Enable VBA access

3)我使用此函数运行宏:


3)I am using this function to run macros:

excel.Application.Run("FilterLoans")

这篇关于Python - 运行Excel宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆