问题
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