How can I confidently set the attributes of dispatched objects using win32com

二次信任 提交于 2019-12-25 12:01:11

问题


I have been struggling to assign an attribute to an object (or is it an instance)

xl=win32com.client.Dispatch('Excel.Application')
xl.Visible=0
srce=xl.Workbooks.Open('myexcelfile')
srce.CheckCompatibility='False'

If I query the srce object about its CheckCompatibility attribute I get a response of 'False'

>>> srce.CheckCompatibility
    False

So I am naively thinking I can save this anyway I want

srce.SaveAs(r'c:\newtttxt14.xls',FileFormat=1)

But when I do that the Compatibility Checker dialog comes up. I hit continue, the file saves and I then check the compatibility again.

>>> srce.CheckCompatibility
    True

So I again try to set it and this time I am successful

srce.CheckCompatibility='False'

I query it once more:

>>> srce.CheckCompatibility
False

Now when I try to save the file the Compatibility Checker Dialog does not appear, the file saves exactly was I want it to.

I need some certainty about being able to set the attribute of srce before I attempt to save the file in another format - can this happen in some other way?

Thanks


回答1:


I'm not sure, but I suspect what's happening is that saving the file in XLS form is resetting the value of CheckCompatibility since the default value of that property is True for Excel 97-2003 binary workbooks.

In any case, there are multiple reasons why Excel might display a dialog when saving a file, not just the compatibility checker. I suspect what you really want is to suppress dialog boxes so that no user interactivity is required when your script saves a file. You can suppress dialog boxes by adding:

>>> xl.DisplayAlerts = False

before your call to srce.SaveAs(...).



来源:https://stackoverflow.com/questions/5208624/how-can-i-confidently-set-the-attributes-of-dispatched-objects-using-win32com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!