Handle Excel Message Box from Within Python using Win32com
I am trying to process some files that were supposed to be xls files but it turns out they are something else (according to some help I received on the python-excel board these are Web Archive files or (Single File Web Page (*.mht, *.mhtml) ). I can open the files with Excel but I get a message that has to be dismissed before I can move forward. The message is - The file you are trying to open filename.xls is in a different format than specified by the file extension. Verify that the file is not corrputed and isfrom a trusted source before opening the file. Do you want to open the file?
If I hit yes, the file opens and it looks like an excel workbook. Some sheet names are truncated. Sure enough, when I hit Save as the default format that shows up is the single file web page.
Now, if I SaveAs and select excel 97-2003 format, rename to my new name and hit the save button the file then opens with Excel with no future questions.
I have been scratching around trying to figure out how to write some code to open and save these as true xls files so I don't have to have someone offshore open and save them.
I have gotten really close but am stuck on one last issue, when I save it with a new fileformat I get a warning box warning me that there will be some minor lost of fidelity. I can't figure out how to supress it and accept that loss of fidelity?
Here is the patched together pieces that seem to work
import win32com.client
xl=win32com.client.Dispatch('Excel.Application')
xl.visible=0 # I have noticed that if I don't set visible to 0 I can't get any response
srce=xl.Workbooks.Open(r'c:\testdd.xls') # testdd is my file that is evidently really not an excel开发者_如何学Python file
srce.SaveAs(r'c:\newtttxt2.xls',FileFormat=1) # this is when the message box pops up
I found reference to anattribute called CheckCompatibility. It seems to be an attribute of a workbook object. And I guess I cannot use it in the form
srce.CheckCompatibility='False'
is because I am not saving the srce object I am saving some type of copy of it??
Back to the mines.
False Hope I was not successful when I tried srce.CheckCompatibility='False'
Sorry for any confusion
Well now I am confused even more than usual this behavior is odd
srce=xl.Workbooks.Open(r'c:\testdd.xls') # I open the file
srce.CheckCompatibility='False' # I try to assign the attribute to srce
srce.SaveAs(r'c:\newtttxt7865.xls',FileFormat=1) #when I SaveAs the compatibility dialog asks if I want to continue
>>> 'asked' # I put this here to reinforce the fact that I was asked
'asked'
>>> srce.CheckCompatibility='False' # again I try to assign the attribute
>>> srce.SaveAs(r'c:\newtttxt78653.xls',FileFormat=1) # I try to save it again
>>> 'did not ask' #it saves this time w/o asking
'did not ask'
>>>
I wonder if I add a make.py file as described in the com help if this would be addressed? I am starting to wonder if the issue is that srce does not know what attributes it has yet, it accepts the assignment but it is not put into play until some even happens? If I sound stupid that is okay.
Please see my answer to your other question on this issue.
The reason it doesn't display a dialog box for the second call to SaveAs(...)
is that the file format is not changing on the second call, the file format only changes on the first call.
What you want is to add
>>> xl.DisplayAlerts = False
before calling SaveAs()
.
精彩评论