如何解决如何使用 win32 将 Python 连接到打开的 Excel 工作簿?
我有多个 Excel 实例正在运行,并且想要连接到在其中一个中打开的特定文件。我尝试按照 Tim Golden 的说明按完整文件名获取工作簿。这是我在 iPython 中所做的:
import win32com.client as win32
xl1 = win32.Dispatch("Excel.Application")
xl1.Visible = False
wb1 = xl1.Workbooks.Add()
wb1.SaveAs(r"C:\Users\[...]\test.xlsx")
xl2 = win32.DispatchEx("Excel.Application")
wb2 = win32.GetObject(r"C:\Users\[...]]\test.xlsm")
结果是:
Traceback (most recent call last)
<ipython-input-22-471d068eb257> in <module>
----> 1 wb2 = win32.GetObject(r"C:\Users\[...]\test.xlsm")
c:\users\[...]\venv\lib\site-packages\win32com\client\__init__.py in GetObject(Pathname,Class,clsctx)
70 return GetActiveObject(Class,clsctx)
71 else:
---> 72 return Moniker(Pathname,clsctx)
73
74 def GetActiveObject(Class,clsctx = pythoncom.CLSCTX_ALL):
c:\users\[...]\venv\lib\site-packages\win32com\client\__init__.py in Moniker(Pathname,clsctx)
85 Python friendly version of GetObject's moniker functionality.
86 """
---> 87 moniker,i,bindCtx = pythoncom.MkParseDisplayName(Pathname)
88 dispatch = moniker.BindToObject(bindCtx,None,pythoncom.IID_IDispatch)
89 return __WrapDispatch(dispatch,Pathname,clsctx=clsctx)
com_error: (-2147221014,'Moniker cannot open file',None)
我做错了什么?
解决方法
以下代码在我的机器上运行良好(Windows 10 64 位,Python Anaconda 3.6.4)。在您的示例中,您使用的文件具有不同的名称。 IDK 如果这是问题所在?
<div className={classes.sectionMobile}>
<IconButton
aria-label="show more"
aria-haspopup="true"
edge="start"
className={classes.menuIcon}
onClick={toggleDrawer(true)}
>
<Menu />
</IconButton>
</div>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。