通过 com 从 Python 调用 Excel 宏/Excel 求解器
我在 2007 xlsm 文件中有一个宏,当调用该宏时,它会创建一个求解器配置(目标、约束等)并在没有任何用户交互的情况下执行求解。从 Excel 手动运行时,效果很好。
我现在想通过 com.Python 来编写这个脚本。但是,当 Python 代码调用求解器宏时:
app.Run("runSolver()")
它失败并显示:
foobar.xlsm 无法解决,错误消息:模型错误。请 验证所有单元格和约束均有效。
如果我在调用 Run()
时在 Python 环境中设置断点,然后从 Excel 手动运行宏,则它可以正常工作,因此求解器配置不会出错。
错误消息在求解器网站上列出,但我认为它不适用于工作表求解手动就好。
此页面表明通过 com.但是,添加
Application.Run "Solver.xla!Solver.Solver2.Auto_open"
As 解算器宏的第一行会产生更通用的结果:
File "c:\python26\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6.py", line 35061, in Run
, Arg26, Arg27, Arg28, Arg29, Arg30
File "c:\python26\lib\site-packages\win32com\client\__init__.py", line 456, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)
那么正确的做法是什么?
编辑:
我将其隔离为可重现的测试用例:
Python代码:
from win32com.client import Dispatch
if __name__ == '__main__':
app = Dispatch("Excel.Application")
app.Visible = True
app.Workbooks.Open(r'C:\path\to\testsolver.xlsm')
app.Run("runsolver()")
Excel文件:http://dl.dropbox.com/u/3142953/testsolver.xlsm(您可以在禁用宏的情况下打开它,并检查module1
来验证它是否安全)。
I have a macro inside a 2007 xlsm file that, when called, creates a solver configuration (target, constraints, ...) and performs the solve without any user interaction. When run manually from Excel, this works fine.
I now want to script this from Python over com. However, when the Python code calls the solver macro:
app.Run("runSolver()")
It fails with:
foobar.xlsm failed to solve, error message: Error in model. Please
verify that all cells and constraints are valid.
If I set a breakpoint in my Python environment at the call to Run()
and then run the macro manually from Excel, it works fine, so the solver configuration can't be wrong.
The error message is listed on the solver website but I don't think it applies as the sheet solves fine manually.
This page suggests that the solver environment is not yet set up when calling through com. However, adding
Application.Run "Solver.xla!Solver.Solver2.Auto_open"
As the first line of my solver macro results in the more generic:
File "c:\python26\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6.py", line 35061, in Run
, Arg26, Arg27, Arg28, Arg29, Arg30
File "c:\python26\lib\site-packages\win32com\client\__init__.py", line 456, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)
So what is the right thing to do?
Edit:
I isolated it down to a reproducable test case:
Python code:
from win32com.client import Dispatch
if __name__ == '__main__':
app = Dispatch("Excel.Application")
app.Visible = True
app.Workbooks.Open(r'C:\path\to\testsolver.xlsm')
app.Run("runsolver()")
Excel file: http://dl.dropbox.com/u/3142953/testsolver.xlsm (you can open it with Macros disabled and inspect the simple sub in module1
to verify it's safe).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于在 MSDN Excel 开发人员论坛。解决方案非常简单:
像这样运行它:而不是像这样运行求解器
您显然必须删除括号并
:去算一下。
I finally found a solution after posting on the MSDN Excel Developers forum. And the solution was maddingly trivial:
Instead of running the solver like so:
You apparently have to drop the brackets and run it like so:
Go figure.