Python / Excel : Turn off Office Excel Compatibili

2019-07-23 19:15发布

A file which has data I need to access is being generated in xlsx format yet I need it to be in xls format for my purpose.

I'm able to use win32com.client to open the file however the save cannot fully complete due to Compatibility Checker dialog pop up which notifies you of loss of formatting/features by going from a xlsx --> xls format.

Here's the code which currently doesn't allow me to save the file as execution hangs waiting for the dialog to close, any help would be much appreciated:

excel = win32com.client.Dispatch('Excel.Application')
excel.DisplayAlerts = False

in_file = u"C:\\Path\\to\\infile\\infile.xlsx"
out_file = u"C:\\Path\\to\\outfile\\outfile.xls"

wb = excel.Workbooks.Open(in_file)

wb.CheckCompatibility = False
wb.DoNotPromptForConvert = True

wb.SaveAs(out_file, FileFormat=56) #Execution hangs here
wb.Close()
excel.Quit()

I've seen other similar posts which mention the methods and attributes I've already set in this script. I've also modified my the registry value to ShowCompatDialog = 0.

2条回答
贪生不怕死
2楼-- · 2019-07-23 19:40

MSDN says on Workbook.DoNotPromptForConvert property: "true to prompt the user to convert the workbook; otherwise, false".

Write in your code:

wb.DoNotPromptForConvert = False
查看更多
该账号已被封号
3楼-- · 2019-07-23 19:49

UPDATE: Solved this issue using this Excel Add-In

For reference: Changing the registry value was working however the values were being reset daily on the internal network which I develop in. Without being able to edit the registry values myself b/c I don't possess the admin rights to do so, the above solution was the only thing that ended up solving my problem.

查看更多
登录 后发表回答