使用python开发宏_Python:使用Python运行Excel宏

在尝试通过Python运行Excel宏时遇到错误:`pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146788248), None)`和`运行时错误 1004`。问题在于宏可能未在工作簿中定义或者宏安全设置禁用了所有宏。解决方案是确保启用所有宏,并正确指定工作簿路径和宏名。提供了一段成功运行Excel宏的Python代码示例。" 122641730,5884820,Python CSV与Pandas处理CSV文件详解,"['python', 'pandas', '数据处理', 'csv']
摘要由CSDN通过智能技术生成

I need to run an Excel macro via python and I always get the following error :

result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146788248), None)

In Excel, it is giving the following error :

Run-time error 1004:

Cannot run the macro ". The macro may not be available in this workbook or all macros may be disabled.

My code is the following :

xl=win32.Dispatch("Excel.Application")

wb=xl.Workbooks.Open(Filename="Path+MyExcelFile.xlsm", ReadOnly=1)

xl.Visible = True

time.sleep(1)

ws=wb.Worksheets("Sheet1")

ws.Cells(4,2).Value='Value1'

ws.Cells(5,2).Value='Value2'

ws.Cells(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值