我使用win32com控制Python中的Visio。
获取和设置形状表值就像:
print(shp.CellsU('PinX').ResultStr(''))
# and
shp.CellsU('PinX').FormulaU = '1'
到目前为止,还不错,但我希望通过重写setter和getter来获得更短的语法,以获得如下内容:
print(shp.PinX)
# and
shp.PinX = '1'
所以我去买了一处房产
ShapeClass = type(shp)
def SetPinX(self,value):
self.CellsU('PinX').FormulaU = value
def GetPinX(self):
return self.CellsU('PinX').ResultStr('')
ShapeClass.PinX = property(GetPinX,SetPinX)
现在奇怪的结果---- getter工作得很好(打印(shp.PinX)给出了期望值),但是设置器不能工作。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\conda\conda\envs\YG_Package_1\lib\site-packages\win32com\client\__init__.py in __setattr__(self, attr, value)
478 try:
--> 479 args, defArgs=self._prop_map_put_[attr]
480 except KeyError:
KeyError: 'PinX'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-28-23f68b65624d> in <module>()
----> 1 shp.PinX= '1'
~\AppData\Local\conda\conda\envs\YG_Package_1\lib\site-packages\win32com\client\__init__.py in __setattr__(self, attr, value)
479 args, defArgs=self._prop_map_put_[attr]
480 except KeyError:
--> 481 raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
482 self._oleobj_.Invoke(*(args + (value,) + defArgs))
483 def _get_good_single_object_(self, obj, obUserName=None, resultCLSID=None):
AttributeError: '<win32com.gen_py.Microsoft Visio 15.0 Type Library.IVShape instance at 0x85710888>' object has no attribute 'PinX'
dir(ShapeClass)可以很好地显示属性PinX。
用自己的类进行测试也是有效的。这样,错误就不会发生在我实现属性的方式上。
我怀疑win32com在设置器被覆盖时有问题。
有谁知道如何解决这个问题吗?
发布于 2019-04-03 02:54:09
win32com.client.DispatchBaseClass
基类使用__setattr__
来拦截all属性设置访问。这会影响您的属性对象;属性设置程序只能由默认的object.__setattr__
实现调用,而不是由win32com
使用的自定义方法调用。
因此,是的,shp.PinX = '1'
将调用DispatchBaseClass.__setattr__('PinX', '1')
,即使在类上定义了名为PinX
的数据描述符,这将失败,因为它只支持COM接口定义的属性。
您必须在这里重写__setattr__
方法,以首先检查可用的属性。您可以是子类DispatchBaseClass
,也可以是特定生成的类,或者我们可以直接对win32com
进行猴子补丁:
import inspect
from win32com.client import DispatchBaseClass
dispatch_setattr = DispatchBaseClass.__setattr__
def allow_datadescriptor_setattr(self, name, value):
# for non-private names, check if the attribute exists on the class
# and is a data descriptor. If so, use object.__setattr__ to permit
# the data descriptor to intercept attribute setting
if name[:1] != '_' and inspect.isdatadescriptor(getattr(type(self), name, None)):
return object.__setattr__(self, name, value)
# private name, or doesn't exist on the class, or not a data descriptor
# invoke the original win32com dispatch __setattr__
dispatch_setattr(self, name, value)
DispatchBaseClass.__setattr__ = allow_datadescriptor_setattr
以上允许任何 method拦截其名称的赋值,而不仅仅是property
对象。
发布于 2019-04-01 04:01:27
您正在混合.Formula和.ResultStr方法。一个牢房可以有:
.Formula .FormulaU .Result .ResultU .ResultStr .ResultStrU
.FormulaForce .FormulaForceU
在您的代码中,您使用.ResultStr获取单元格内容,使用.FormulaU设置单元格。我建议您查看Visio,以了解所有这些之间的差异。如果单元格具有受保护的公式/值,则需要使用.FormulaForce方法。
https://stackoverflow.com/questions/55431363
复制相似问题