python win32 COM closing excel workbook

后端 未结 3 909
猫巷女王i
猫巷女王i 2020-12-09 05:52

I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open.

3条回答
  •  再見小時候
    2020-12-09 06:05

    You can also try to use the following code:

    excel = Dispatch("Excel.Application")
    excel.Visible = False
    workbook = excel.Workbooks.Open(fileName)
    
    # with saving
    excel.DisplayAlerts = False
    if saveAs:
        excel.ActiveWorkbook.SaveAs(fullFileNameToSave)
    else:
        excel.ActiveWorkbook.Save()
    excel.Quit()
    
    #without saving
    
    map(lambda book: book.Close(False), excel.Workbooks)
    excel.Quit()
    

提交回复
热议问题