Python / Excel : Turn off Office Excel Compatibility Checker Programmatically

旧时模样 提交于 2019-12-13 16:42:07

问题


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.


回答1:


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

Write in your code:

wb.DoNotPromptForConvert = False



回答2:


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.



来源:https://stackoverflow.com/questions/41578066/python-excel-turn-off-office-excel-compatibility-checker-programmatically

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